Hardware Reference
In-Depth Information
break # leave the loop
elif direction1 == “right”:
print(“You walk to the beach but remember you do not ;
have any swimwear.”)
break # leave the loop
else:
print(“You think for a while”)
In this code, shown also in Figure 5-11, you can see in bold text the Python words
while True: added before the user input question, and break added within the
conditionals for left and right. he while True: condition will loop the question
over and over until the player enters either left or right so that the game does not
end if the player types anything else.
FIGURE 5-11 Using a while loop in the Python adventure game
A
Indentation refers to how far from the margin a line of code is typed. Indentation
of code in Python is very important, especially when you begin to add more
structure to your code by using conditionals and loops. In Adventures 3 and 4, you
used conditionals and loops in Scratch. When you added a forever loop, you
placed individual blocks inside it. These blocks inside the loop were indented from
the rest of the statement. It is similar in Python. After you type while True : the
next line should be indented, otherwise your code may not work. The same is
true of the conditionals if , else and elif .
Search WWH ::




Custom Search