我在运行代码时收到 EOF 错误,我该怎么办?

I get an EOF error when running my code, what can i do?

提问人:Piolm 提问时间:11/3/2022 更新时间:11/13/2022 访问量:133

问:

基本上我有一个程序,你必须设置老师的耐心,然后数“一”、“二”、“三”、“四”、“一”等,当你失败时,写下“连胜是......,但你失败了”,在耐心达到 0 后,老师说“今天够了”,然后忽略所有输入,但是当我得到这样的命令时:

2
one
two
three
four
one
twu
one
two
three
three

在某些情况下,即使我在整数中转换了所有需要的输入,我也会在启动它时收到EOF错误。 法典:

patience = int(input())
a = input()
streak = 0
mistake = False
one = "one"
two = "two"
three = "three"
four = "four"
nextNum = one
while 1:
    if a == nextNum:
        mistake = False
        streak += 1
        if nextNum == one:
            nextNum = two
        elif nextNum == two:
            nextNum = three
        elif nextNum == three:
            nextNum = four
        elif nextNum == four:
            nextNum = one
    elif not mistake:
        mistake = True
        nextNum = one
        patience -= 1
        if patience == 0:
            print("The counting streak was " + str(streak) + ", but you failed.")
            print("Enough for today.")
        if patience >= 1:
            print("The counting streak was " + str(streak) + ", but you failed.")
        streak = 0
    a = input()

我不知道这里有什么问题,因为它就像一个随机的机会,我不明白为什么。

Python EOF

评论

0赞 user19077881 11/3/2022
尝试使用代码,您将看到在单步执行时会发生什么。https://pythontutor.com/visualize.html#mode=edit
0赞 D.L 11/3/2022
每个老师从多少耐心开始?

答:

0赞 Naga 11/13/2022 #1

原因

让我们看一下循环,当你第一次猜对时,变量设置为 。因此,如果您第二次猜错了,则设置为 .第三次,如果你再猜错,什么都不会发生。cause 不等于 变量 ,并且设置为 更早。因此,与其使用 .这样,如果猜对了 ELSE,则运行 ELSE 情况。whilemistakefalsemistaketruenextNumamistakeTrueelifelsenextNum

溶液

我建议将 . 另外,不要忘记删除布尔变量的其他三个引用,它只是一个不必要的行。elif not mistake:else:mistake =