1

所以我是一个绝对的初学者,并且通过 Zed Shaw 的 Learn Python The Hard Way 工作。出于某种原因,今天当我运行一个程序时,我随机得到不同的输出。下面是我的代码的一部分以及一些不一致的输入/输出。我已经连续尝试了多次,有时代码可以正常工作并调用下一个函数,有时它会跳过大部分代码。

这是我的代码运行不一致...

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

这是一些不一致的输出...这里我运行程序并在提示符下使用输入“left”。

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!

在这里我立即做同样的事情,这次它运行但输出不同。

 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?  

我知道在 C++ 中创建新变量时可能是堆栈与堆的问题,但我在同一台计算机上找不到 Python 函数的任何答案。我还重新输入了我的代码,以防出现一些我没有看到的缩进错误。有几次,当我继续输入“take honey”时,我能够得到正确的输出,但这只能在一半时间内起作用,而“taunt bear”根本不起作用。它只是直接传递到其他地方。有什么想法吗?这有意义吗?

4

2 回答 2

1

通过查看本练习的代码,您肯定在其中一次尝试中拼错了“left”,请注意,这可能只是不必要的大写或开头或结尾的意外空格。

这是有问题的代码:

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.")

如果你准确地输入“left”并按下回车,你应该总是进入熊房间。

于 2013-05-29T19:22:34.093 回答
0

“左”或“右”之后的尾随空格会让你饿死。:)

于 2013-05-29T19:41:25.230 回答