Hello. I’m trying to program a python forest game where you control the outcome by choosing the direction at which your headed in a forest using either LEFT or RIGHT. I have made it so that if you choose to go LEFT, you automatically make it out of the forest and choosing RIGHT more than a set number of times (4) will lead you to the end of the forest. Any other inputs other than LEFT or RIGHT will execute an else statement that prints out ‘go LEFT or RIGHT’ in it’s respective manner. But it seems that when trying to choose the right way out will leave you in a continuous inputting loop. Additionally, after choosing right a number of times, for some unknown reason it prints out my else statement ‘go LEFT or RIGHT’. What should I correct? As always it would be advisable to run the code yourself to experience it hands on.
I think you can approach this in several ways but the following should work.
Your break
statements break out of the inner loop, but the outer loop always runs after that.
Instead of while True:
, create a variable is_running = True
and change its value to False
just before the break
. This shows you the basic logic:
is_running = True
while is_running:
print('outer is running')
while True:
print('inner is running')
is_running = False
break
Output:
outer is running
inner is running
could you elaborate, I’m a bit low IQ
Please, tell me what part is not clear and I’ll try to explain it in other words