1

I'm trying to do two things:

  1. 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.

  2. 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?

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • Please be aware that a malicious user could call the blackdoor function until your stack overflows, potentially allowing for arbitrary code insertion. – Spencer Rathbun Jun 01 '12 at 20:03

3 Answers3

1

You're recursively calling blackdoor() but you are not returning the result from the call. You need to return blackdoor() to get the result back to the caller.

A simple change should do it;

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"
    return blackdoor()
return decision
AlG
  • 14,697
  • 4
  • 41
  • 54
  • I tried this as I tried Aladdin's code, for some reason it keep having to enter a value twice before the loop exits. Any idea why? – JWilkerson Jun 01 '12 at 20:33
  • From your design, it looks like you have no choice. You'd have to enter a 'wrong' choice to get into the recursive call, then enter a 'right' choice to exit it. My recommendation would be to rewrite it and remove the recursion. – AlG Jun 05 '12 at 11:43
1

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:

def blackdoor():
    end_condition = False
    while not end_condition:
        end_condition = True
        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"
            end_condition = False
    return decision


decision = blackdoor()
Mahmoud Aladdin
  • 536
  • 1
  • 3
  • 13
0

I am assuming that you are having many of these decision points throughout your game and you will get lost trying to embed them in functions. If this is the case, then you should explore using a simple Finite State Machine (FSM).

A list of FSM implementations exists on PythonWiki.

Using an FSM seems overkill if you have only one or two actions, but you will find it needlessly difficult to program even a simple adventure game without the help of one.

Update I found a useful answer on Python and FSM for future reference.

Community
  • 1
  • 1
daedalus
  • 10,873
  • 5
  • 50
  • 71
  • I read about these before, but I literally started programming in python like 3 days ago. lol – JWilkerson Jun 01 '12 at 20:38
  • Yes, it can be daunting. Keep this suggestion in mind after you have experimented with these functions for a while. When you have more confidence you can try a simple tutorial or an introductory book on game programming to learn more about FSMs. +1 as welcome to SO and to Python. – daedalus Jun 01 '12 at 20:43