Making a loop based on user input, then saving the result for a MUD in Python

879 views Asked by At

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?

3

There are 3 answers

0
Mahmoud Aladdin On

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()
2
daedalus On

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.

2
AlG On

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