简体   繁体   中英

Running same program in terminal with different output

So I am an absolute beginner and working through Zed Shaw's Learn Python The Hard Way. For some reason today when I am running a program I am getting different outputs randomly. Below is aa portion of my code as well as some of the inconsistent input/output. I have tried this multiple times in a row and sometimes it the code works properly and calls the next function and sometimes it skips over the majority of it.

Here is my code that isn't running consistently ...

def bear_room():    
    print "There is a bear in here."
    print " The bear has a bunch of honey."
    print " The fat bear is in front of another door."
    print " How are you going to move the bear?"
    bear_moved = False 

    while True:
        next = raw_input(">")

        if next == "take honey":                        
            dead("The bear looks at you and slaps your face off.")
        elif next == "taunt bear" and not bear_moved:
            print "The bear has moved from the door. You can go through it now."
            bear_moved = True
        elif next == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        elif next == "open door" and bear_moved:
            gold_room()
        else: 
            print " I have no idea what that means."

Here is some of the inconsistent output... Here I run the program and use the input "left" at the prompt.

Theresa-Beckers-MacBook-Pro:Summer 2013 Python leafgirl12$ python ex35.py   
You are in a dark room.  
There is a door to your right and left  
Which one do you take?  
>left  
You stumble around the room until you starve. Good job!

Here I do the same thing immediately after and this time it runs through but the output is different.

 Theresa-Beckers-MacBook-Pro:Summer 2013 Python leafgirl12$ python ex35.py   
 You are in a dark room.  
 There is a door to your right and left  
 Which one do you take?  
 >left  
 There is a bear in here.  
 The bear has a bunch of honey.  
 The fat bear is in front of another door.  
 How are you going to move the bear?  

I know in C++ when creating new variables it can be a matter of stack vs heap, but I con't find any answers for Python functions on the same computer. I have also retyped my code in case there is some indentation error that I am not seeing. A few times I have been able to get the correct output when I continue and type "take honey" but this only works half the time and "taunt bear" has yet to work at all. It just passes straight through to the else. Any thoughts? Does this makes sense?

From looking at the code for this exercise , you must have misspelled "left" on one of the attempts, note that this could have been something as small as unnecessary capitalization or an accidental space at the beginning or end.

Here is the code in question:

def start():
    print "You are in a dark room."
    print "There is a door to your right and left."
    print "Which one do you take?"

    next = raw_input("> ")

    if next == "left":
        bear_room()
    elif next == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve.")

If you type in "left" exactly and press enter, you should always enter the bear room.

Trailing whitespace after "left" or "right" will starve you to death. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM