简体   繁体   English

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

[英]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. 所以我是一个绝对的初学者,并且通过Zed Shaw的《学习Python的艰难方法》进行研究。 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. 这是一些不一致的输出...在这里,我运行程序并在提示符下使用输入“ 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!

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. 我知道在C ++中创建新变量时,它可能是堆栈还是堆的问题,但是我在同一台计算机上找不到Python函数的任何答案。 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. 几次,当我继续并输入“ take honey”时,我已经能够获得正确的输出,但这只能工作一半时间,而“嘲讽熊”却根本无法工作。 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. 通过查看此练习的代码 ,您肯定在其中一种尝试中拼写了“ left”,请注意,这可能只是不必要的大写字母,也可能是开头或结尾的意外空格。

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. 如果您完全输入“ left”并按Enter,则应始终进入熊室。

Trailing whitespace after "left" or "right" will starve you to death. 在“左”或“右”后跟随空白将使您饿死。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从不同的终端向运行在linux终端窗口中的python程序的python程序提供raw_input,它们都在同一UNIX上运行 - How to supply raw_input to a python program running on a terminal window in linux from a different terminal, all running on same UNIX 打开在Python中运行相同程序的终端 - Opening a terminal running the same program in Python 如何使在一个终端中运行的程序的输出成为另一终端的输入? - How to make the output of program running in one terminal be the input of another terminal? Python:启动终端程序并在运行时解析其输出 - Python: start terminal program and parse its output while it's running 同一程序在线程模块中输出不同的输出 - same program different output in threading module 文件输出与终端输出不同 - File output different to terminal output 在终端中运行 Python 程序时出错 - Error running a Python program in Terminal 将四个并行运行的python程序的输出保存到不同的日志文件 - Save output of four parallely running python program to different log files BeautifulSoup:终端输出在不同的行中 - BeautifulSoup : Terminal output are in different lines 每次运行相同的代码后得到的输出略有不同 - Getting slightly different output every time after running the same code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM