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

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

问题描述:

所以我是一个绝对的初学者,通过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." 

下面是一些不一致的输出... 这里我运行该程序,并使用“左”输入的提示。

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++中创建新的变量时,它可以堆栈VS堆的问题,但我续找到在同一台计算机上的Python功能的任何答案。我还重新输入了我的代码,以防出现一些我看不到的缩进错误。有几次,当我继续输入“take honey”时,我能够得到正确的输出结果,但这只能用一半的时间,而“嘲讽的熊”还没有起作用。它只是直接传递给其他人。 有什么想法?这有道理吗?

+0

您已在此发布错误代码。它不是一贯运行的'bear_room'代码;它是不一致运行的'start'代码。 – abarnert

+0

当你输入“left”时,你是否意外地在之前或之后放置了空白区域? – cmd

+0

调试这个问题的最好方法是添加类似'print'的东西你在每个'raw_input()'语句后面说'{!r}''format(next)',所以你可以看到_exactly_你输入的内容。 – abarnert

“左”或“右”后面的空格将使您饿死。 :)

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

+0

...或者,也许她在复制代码时拼写错误“left”亲自进入她的节目。她向我们展示了她的成绩单,她清楚地输入了“左”字,除非有一些不可打印的字符,但是她没有向我们显示带有该字符串的代码。 – abarnert

+0

哇。非常感谢你的回答。作为一个新手,并且在一场新秀赛上的错误让我的头撞在墙上的时间太长了。尾随白色空间。 :/ sheesh。再次感谢! – tjustbecause