I'm pretty new to programming, but I've got a quick question. I'm trying to write a sort of "choose your own adventure" game, but I've run into a problem. I'm only really as far into if
statements in the code, but I want to be able to send the user back to previous code when they type something.
For example:
print "You are in a room with two doors to either side of you."
choiceOne = raw_input["Which way will you go?"]
choiceOne = choiceOne.lower[]
if choiceOne = "r" or choiceOne = "right":
print "You go through the right door and find yourself at a dead end."
elif choiceOne = "l" or choiceOne = "left":
print "You go through the left door and find yourself in a room with one more door."
else:
print "Please choose left or right."
In the if
statement, I want to send the user back to choiceOne
's raw_input[]
. In the elif
statement, I want to give the user the option to either proceed through the next door, or return to the first room to see what secrets the other door may hold. Is there any way to do this? I don't care if the way is complicated or whatever, I just want to get this working.
mkrieger1
15.6k4 gold badges45 silver badges57 bronze badges
asked Sep 25, 2014 at 17:51
5
Are you looking for a while
loop?
I think that this website explains it very well: //www.tutorialspoint.com/python/python_while_loop.htm
count = 0
while [count < 9]:
print 'The count is:', count
count = count + 1
print "Good bye!"
→
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
answered Sep 25, 2014 at 17:55
kaykay
24.7k10 gold badges96 silver badges139 bronze badges
Use a while
loop:
while True:
print "You are in a room with two doors to either side of you."
choice_one = raw_input["Which way will you go?"].lower[]
if choice_one == "r" or choice_one == "right":
print "You go through the right door and find yourself at a dead end."
continue # go back to choice_one
elif choice_one == "l" or choice_one == "left":
print "You go through the left door and find yourself in a room with one more door."
choice_two = raw_input["Enter 1 return the the first room or 2 to proceed to the next room"]
if choice_two == "1":
# code go to first room
else:
# code go to next room
else:
print "Please choose left or right."
You need to use ==
for a comparison check, =
is for assignment.
To
break the loop you could add a print outside the loop print "Enter e to quit the game"
:
Then in your code add:
elif choice_one == "e":
print "Goodbye"
break
answered Sep 25, 2014 at 18:00
2
Not the answer you're looking for? Browse other questions tagged python loops or ask your own question.
break
and continue
allow you to control the flow of your loops. They’re a concept that beginners to Python tend to misunderstand, so pay careful attention.
Using break
The break
statement will completely break out of the current loop, meaning it won’t run any more of the statements contained inside of it.
>>> names = ["Rose", "Max", "Nina", "Phillip"]
>>> for name in names:
... print[f"Hello, {name}"]
... if name == "Nina":
... break
...
Hello, Rose
Hello, Max
Hello, Nina
break
completely breaks out of the loop.
Using continue
continue
works a little differently. Instead, it goes back to the start of the loop, skipping over any other statements contained within the loop.
>>> for name in names:
... if name != "Nina":
... continue
... print[f"Hello, {name}"]
...
Hello, Nina
continue
continues to the start of the loop
break
and continue
visualized
What happens when we run the code from this Python file?
# Python file names.py
names = ["Jimmy", "Rose", "Max", "Nina", "Phillip"]
for name in names:
if len[name] != 4:
continue
print[f"Hello, {name}"]
if name == "Nina":
break
print["Done!"]
Results
See if you can guess the results before expanding this section.
Using break
and continue
in nested loops.
Remember, break
and continue
only work for the current
loop. Even though I’ve been programming Python for years, this is something that still trips me up!
>>> names = ["Rose", "Max", "Nina"]
>>> target_letter = 'x'
>>> for name in names:
... print[f"{name} in outer loop"]
... for char in name:
... if char == target_letter:
... print[f"Found {name} with letter: {target_letter}"]
... print["breaking out of inner loop"]
... break
...
Rose in outer loop
Max in outer loop
Found Max with letter: x
breaking out of inner loop
Nina in outer loop
>>>
break
in the inner loop only breaks out of the inner loop! The outer loop continues to run.
Loop Control in while
loops
You can also use break
and continue
in while
loops. One common scenario is running a loop forever, until a certain condition is met.
>>> count = 0
>>> while True:
... count += 1
... if count == 5:
... print["Count reached"]
... break
...
Count reached
Be careful that your condition will eventually be met, or else your program will get stuck in an infinite loop. For production use, it’s better to use asynchronous programming.
Loops and the return
statement
Just like in functions, consider the return
statement the hard kill-switch of the loop.
>>> def name_length[names]:
... for name in names:
... print[name]
... if name == "Nina":
... return "Found the special name"
...
>>> names = ["Max", "Nina", "Rose"]
>>> name_length[names]
Max
Nina
'Found the special name'