gpt4 book ai didi

python - 在具有不同输出的终端中运行相同的程序

转载 作者:太空宇宙 更新时间:2023-11-03 18:58:03 26 4
gpt4 key购买 nike

所以我是一个绝对的初学者,正在学习 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 函数的任何答案。我还重新输入了我的代码,以防出现一些我没有看到的缩进错误。有几次,当我继续输入“take honey”时,我能够得到正确的输出,但这只能在一半的时间内起作用,而“taunt bear”则根本不起作用。它只是直接传递到其他。有什么想法吗?这有道理吗?

最佳答案

查看the code for this exercise ,您一定在其中一次尝试中拼错了“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”并按回车键,你应该总是进入熊室。

关于python - 在具有不同输出的终端中运行相同的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16822115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com