The Continue Command
The
continue
command stops executing code in the loop and starts at the top of the loop again. Essentially, we want to kick the user back to the original question.EXAMPLE :
while True:
print("You are in a corridor, do you go left or right?")
direction = input("> ")
if direction == "left":
print("You have fallen to your death")
break
elif direction == "right":
continue
else:
print("Ahh! You're a genius, you've won")
NOTE :
The
else
statement refers to any input besides left or right (up or esc). Since the user is a winner, we do not want to use break
or it would say they have failed. EXIT COMMAND LINE
The previous code continues to loop even after the user has won. Let's fix that with the
exit()
commandEXAMPLE:
print("Let's play chutes and ladders. Pick ladder or chute.")
while True:
print("Do you want to go up the ladder or down the chute?")
direction = input("> ")
if direction == "chute"
print("Game over!")
break
elif direction == "ladder":
continue
else:
print("Game over!")
exit
print("Thanks for playing!")
Comments
Post a Comment