0

次のコードの 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
4

3 に答える 3

4

プログラムは、標準入力からユーザー入力を読み取ります。codepad.org で実行する場合、ユーザー入力はなく、標準入力から読み取ろうとするとEOFError.

代わりにサイト ideone.com を使用すると、ユーザー入力を指定できます。ただし、インタラクティブではなく、事前にすべての入力を提供する必要があります。プログラムが提供された入力の最後を超えて読み取ろうとすると、EOFError再び取得されます。

于 2012-05-31T17:15:26.000 に答える
0

Python 2.x を使用していると仮定すると、次のようになります。

raw_input

ではありませんinput

raw_inputinputPython 3.x でに変更されました

于 2012-05-31T17:09:29.070 に答える
0

技術的には、これはエラーではなく、例外です。この例外は、組み込み関数の 1 つである場合に発生します。最も一般的なのは、

input()

データを読み取らずにファイルの終わり (EOF)を返します。私たちのプログラムがやろうとすることは、何かを取得して変更することだけである場合があります。ただし、フェッチできない場合は、この例外が発生します。

于 2018-10-17T05:43:25.080 に答える