我在以下代码的第87行得到了一个EOFError:
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中运行了这个程序但是当我使用这个站点时会弹出这个错误:
你的猜测是什么?
Traceback (most recent call last):
Line 88, in <module>
guess=input("What is your guess? ")
EOFError
答案 0 :(得分:4)
您的程序从标准输入读取用户输入。在codepad.org上运行时,没有用户输入,尝试从标准输入读取将导致EOFError
。
如果您使用网站ideone.com,它允许您指定用户输入。但是,您必须提前提供整个输入,而不是交互式提供。如果您的程序试图读取所提供输入的结尾,您将再次获得EOFError
。
答案 1 :(得分:0)
假设您使用的是Python 2.x,它应该是:
raw_input
而不是input
。
raw_input
已更改为input
答案 2 :(得分:0)
从技术上讲,这不是错误,而是异常。当内置功能之一(最常见)时会引发此异常
input()
返回文件结束(EOF),而不读取任何数据。有时我们的程序试图做的就是获取某些东西并对其进行修改。但是 当无法获取时,将引发此异常。