首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python中的EOFError

python中的EOFError
EN

Stack Overflow用户
提问于 2012-06-01 01:06:52
回答 3查看 11.4K关注 0票数 0

我在下面代码的第87行得到了一个EOFError:

代码语言:javascript
复制
import random
def printDice(diceList):
    upperLine=" _____     _____     _____     _____     _____"
    line1="|" 
    line2="|"
    line3="|"
    lowerLine=" -----    -----    -----    -----    -----"
    for i in range(len(diceList)):
        if(diceList[i]==1):
            line1+="     "
        elif(diceList[i]==2):
            line1+="*    "
        elif(diceList[i]==3):
            line1+="*    "
        elif(diceList[i]==4):
            line1+="*   *"
        elif(diceList[i]==5):
            line1+="*   *"
        else:
            line1+="*   *"
        if(i==4):
            line1+="|"
        else:
            line1+="|  |"
    for i in range(len(diceList)):
        if(diceList[i]==1):
            line2+="  *  "
        elif(diceList[i]==2):
            line2+="     "
        elif(diceList[i]==3):
            line2+="  *  "
        elif(diceList[i]==4):
            line2+="     "
        elif(diceList[i]==5):
            line2+="  *  "
        else:
            line2+="*   *"
        if(i==4):
            line2+="|"
        else:
            line2+="|  |"
    for i in range(len(diceList)):
        if(diceList[i]==1):
            line3+="     "
        elif(diceList[i]==2):
            line3+="    *"
        elif(diceList[i]==3):
            line3+="    *"
        elif(diceList[i]==4):
            line3+="*   *"
        elif(diceList[i]==5):
            line3+="*   *"
        else:
            line3+="*   *"
        if(i==4):
            line3+="|"
        else:
            line3+="|  |"
    print upperLine
    print line1
    print line2
    print line3
    print lowerLine

tellMe="N"
print

print "The purpose of the game is to figure out the rule."
print "I can tell you three things:\n1. The name of the game is petals around a rose,  the name is important.\n2. I can tell you whether or not your guess is right and the score of the dice.\n3. I can tell you that the score is always even or 0"
print
print "At any time you can quit by typing an odd number"
go="Y"
wrongGuesses=0
while(go=="Y"):
    diceList=[]
    score=0
    rightWrong="N"
    for i in range(5):
         diceList.append(random.randrange(1,7))
    for i in range(5):
        if(diceList[i]==3):
            score+=2
        elif(diceList[i]==5):
            score+=4
    printDice(diceList)
    print
    while(rightWrong=="N"):
        guess=input("What is your guess? ")
        if(guess%2==1):
            break
        if(guess!=score):
            print "Wrong"
            wrongGuesses+=1
            tellMe=raw_input("Tell you (Y or N)? ")
            tellMe=tellMe.upper()
            if(tellMe=="Y"):
                print "The score was "+str(score)+"."
                rightWrong="Y"
            else:
                rightWrong="Y"
            print "Right"
    if(wrongGuesses%13==0 and wrongGuesses!=0):
        print"The name is very important."
    if((wrongGuesses==30) and (wrongGuesses!=0)):
        print "The maximum score is 20."
    print

我正在使用站点codepad.org来运行这个程序。我实际上已经在IDE中运行了这个程序,但是当我使用这个站点时,会弹出这个错误:

你的猜测是什么?

代码语言:javascript
复制
Traceback (most recent call last):
  Line 88, in <module>
    guess=input("What is your guess? ")
EOFError
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-06-01 01:15:26

您的程序从标准输入中读取用户输入。在codepad.org上运行它时,没有用户输入,尝试从标准输入读取将导致EOFError

如果您改用站点ideone.com,它允许您指定用户输入。但是,您必须提前提供全部输入,而不是以交互方式提供。如果您的程序试图读取超过提供的输入的末尾,您将再次获得EOFError

票数 4
EN

Stack Overflow用户

发布于 2012-06-01 01:09:29

假设您使用的是Python 2.x,它应该是:

raw_input

而不是input

在Python3.x中将raw_input更改为input

票数 0
EN

Stack Overflow用户

发布于 2018-10-17 13:43:25

从技术上讲,这不是一个错误,而是一个异常。当一个内置函数出现时,通常会引发此异常

代码语言:javascript
复制
input()

返回End-Of-File(EOF),不读取任何数据。有时我们的程序所要做的就是获取一些东西并修改它。但是当无法获取时,它将引发此异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10838090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档