在不同输出的终端中运行相同的程序

时间:2013-05-29 19:17:21

标签: python

所以我是一个绝对的初学者,并通过Zed Shaw的学习Python艰难的方式工作。 出于某种原因今天,当我运行程序时,我随机获得不同的输出。下面是我的代码的一部分以及一些不一致的输入/输出。我连续多次尝试过这种方法,有时代码正常工作并调用下一个函数,有时它会跳过大部分代码。

这是我的代码没有一直运行......

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函数的任何答案。我还重新输入了我的代码,以防有一些我没有看到的缩进错误。有几次,当我继续输入“拿蜂蜜”时,我能够得到正确的输出,但这只有一半的时间,“嘲讽熊”还没有起作用。它直接传递给其他人。 有什么想法吗?这有道理吗?

2 个答案:

答案 0 :(得分:1)

the code for this exercise看,你必须在其中一次尝试中拼错“左”,注意这可能是一些小到不必要的大写或开头或结尾的意外空间。

以下是相关代码:

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”并按Enter,则应始终进入熊房。

答案 1 :(得分:0)

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

相关问题