I'm trying to do two things:
Make a loop in python that provides the user a prompt with 4 possible options; entering "1", "2", "3", or anything else. If the user selects 1,2,or 3 they are presented with text. If a user enters anything else they are presented with text, and the prompt all over again. This repeats until they enter 1,2,or 3.
I want to then take that input from the user to use outside of that loop and continue through the game.
My solution thus far: Before I post my code I'll describe it, I've basically placed all the code I want in the loop within a function with no arguments. I then call that function within the else statement.
What the code is doing: The code is looping the way I want it to, but I don't know how to "break out" of the loop to continue based off of what the user entered. I know it has to be a return, but I guess I don't know where to place it.
What I've tried: post calling the function I entered:
if blackdoor(decision) == "1":
And continue from there, but that isn't working.
The Code:
def blackdoor():
print """After having recently died you awake to find yourself standing in an all white room with a black door that seems to go into the sky forever. What do you do?\n1.Touch the door.\n2.Shout at the door.\n3.Stare at the door."""
decision = raw_input("> ")
if decision == "1":
print "You touch the door. It is as cold as ice. You can feel a vibration pulsing from the door through your body.\n"
elif decision == "2":
print "You shout 'Hello?! Is anybody there?!' at the door. But nothing responds. Your voice echoes off in the distance.\n"
elif decision == "3":
print "You stare at the door intensely. You envision it opening when all of a sudden you get the feeling of something staring back at you.\n"
else:
print "You don't follow instructions very well, do you?\n"
blackdoor()
return decision
blackdoor()
How can I pull the input for decision out to use it as a condition to keep the game going?
According to my understanding to your problem, your code seems fine, however, it'll probably run out of stack due to numerous recursion, if you haven't set the recursion limit to a large number.
I'd go for an iterative solution, as follows: