【问题标题】:Can't figure out EOF error in python [closed]无法弄清楚python中的EOF错误[关闭]
【发布时间】:2014-05-14 21:59:17
【问题描述】:

我正在做Learn Python the Hard Way 一书中的练习35,我似乎无法弄清楚这个错误。这可能很愚蠢,但我似乎无法弄清楚。

错误出现在第 75、69 和 26 行,模块 start()、start bear_room() 和 bear_room next2 = raw_input("> ")

from sys import exit


def gold_room():
    print "This room is full of gold. How much do you take?"
    next1 = raw_input("< ")
    if "0" in next1 or "1" in next1:
        how_much = int(next1)
    else:
        dead("Man, learn to type a number.")

    if how_much < 50:
        print "Nice you're not greedy, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")


def bear_room():
    print "There is a bear 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:
        next2 = raw_input("> ")
    if next2 == "take honey":
        dead("The bear looks at you then pimp slaps your face off.")
    elif next2 == "taunt bear" and not bear_moved:
        print "The bear has moved from the door. You can go through it now."
        bear_moved = True
    elif next2 == "taunt bear" and bear_moved:
        dead("The bear gets pissed off and chews your crotch off.")
    elif next2 == "open door" and bear_moved:
        gold_room()
    else:
        print "I got no idea what that means."


def cthulu_room():
    print "Here you see the great evil Cthulu."
    print "He, it, whatever stares at you and you go insane."
    print "Do you flee for your life of eat your head?"
    next3 = raw_input("> ")
    if "head" in next3:
        dead("Well that was tasty!")
    elif "flee" in next3:
        start()
    else:
        cthulu_room()


def dead(why):
    print why, "Good job!"
    exit(0)


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?"
    next4 = raw_input("> ")
    if next4 == "left":
        bear_room()
    elif next4 == "right":
        cthulu_room()
    else:
        dead("You stumble around the room until you starve.")


start()

【问题讨论】:

  • 你真的有那么多空白,还是只是你把它复制到 Stackoverflow 的方式?
  • 在运行您的代码时,我没有得到EOFError,尽管我必须修复缩进,例如bear_room。你能提供完整的追溯吗?
  • 我很确定 Learn Python the Hard Way 不涉及将代码转储到 Stack Overflow
  • @FrédéricHamidi 虽然这不是世界上最好的问题 - 至少这是一个尝试过的问题...... OP 似乎愿意改进 - 让我们至少给他们一些喘息的空间?
  • @JonClements,我知道你来自哪里,但发问者甚至没有提供他们收到的实际错误,尽管有追溯。这些是这里缺少的基本反射,尽管它们不应该是。在这种情况下,我们不能向每个用户伸出援手,we have tried that beforeit does not scale well

标签: python-2.7 eof


【解决方案1】:

我没有遇到您的具体错误,但是根据您的描述很难判断我是否遇到了错误。将来,您应该发布带​​有行异常的堆栈跟踪。

我发现你的代码中有一个无限循环。这部分:

while True:
    next2 = raw_input("> ")
if next2 == "take honey":
# snip

永远不会退出。 True 将始终为 True,因此它只会不断提示您使用 raw_input。通过将该函数的其余部分更改为如下所示...

while True:
    next2 = raw_input("> ")
    if next2 == "take honey":
        dead("The bear looks at you then pimp slaps your face off.")
    elif next2 == "taunt bear" and not bear_moved:
        print "The bear has moved from the door. You can go through it now."
        bear_moved = True
    elif next2 == "taunt bear" and bear_moved:
        dead("The bear gets pissed off and chews your crotch off.")
    elif next2 == "open door" and bear_moved:
        gold_room()
    else:
        print "I got no idea what that means."

...我们告诉 Python 获取存储在 next2 中的提示结果并评估其余的检查。这样,它将通过调用诸如 dead() 或 gold_room() 等其他函数来退出该函数。即使 while True 条件从未退出,这也会退出 bear_room() 函数。当条件计算为 False 时退出 While 循环。因为 True 永远不会等于 False,所以我们必须以其他方式退出,例如调用另一个函数,如 dead(),该函数将通过调用 exit() 终止。

【讨论】:

  • 这就解释了,无限循环。当我到达 Bear_room() 函数需要 raw_input 的那部分时,无论何时输入,都不会发生任何事情。当我按 ctrl+Z 终止程​​序时,我只会得到错误。那个空白就是它在书中的布局,我只是坚持它,所以我的行号与练习中的书相匹配。感谢您的帮助!
  • 我只是没有缩进 While true 之后的所有行。现在可以正常使用了!
  • 不客气,@user3466180。如果我的回答对您有帮助,请考虑点赞或接受 (stackoverflow.com/help/someone-answers)
猜你喜欢
  • 1970-01-01
  • 2013-07-31
  • 2022-01-04
  • 2014-11-07
  • 2016-05-04
  • 1970-01-01
  • 2015-10-26
  • 2012-08-23
  • 1970-01-01
相关资源
最近更新 更多