[英]Trying to load and edit a pickled dictionary, getting an EOFError
我正在嘗試修改作為教程一部分的書籍中的瑣事程序; 我需要使用腌制的字典保存球員的姓名和分數。 我已經使用單獨的程序創建了dat文件,以避免讀取不存在的文件。
這是瑣事程序的代碼。
#Trivia Challenge
#Trivia game that reads a plain text file
import sys
def open_file(file_name, mode):
"""Open a file"""
try:
the_file = open(file_name, mode)
except IOError as e:
print("Unable to open the file", file_name, "Ending program.\n", e)
input("\n\nPress the enter key to exit.")
sys.exit()
else:
return the_file
def next_line(the_file):
"""Return next line from the trivia file, formatted."""
line = the_file.readline()
line = line.replace("/", "\n")
return line
def next_block(the_file):
"""Return the next block of data from the triva file."""
category = next_line(the_file)
question = next_line(the_file)
answers = []
for i in range(4):
answers.append(next_line(the_file))
correct = next_line(the_file)
if correct:
correct = correct[0]
explanation = next_line(the_file)
value = next_line(the_file)
return category, question, answers, correct, explanation, value
def welcome(title):
"""Welcome the player and get his or her name."""
print("\t\tWelcome to Trivia Challenge!\n")
print("\t\t", title, "\n")
def saving(player_name):
import pickle
f = open("trivia_scores.dat", "rb+")
highscores = pickle.load(f)
if player_name in highscores and score > highscores[player_name]:
highscores[player_name] = score
pickle.dump(highscores, f)
elif player_name not in highscores:
highscores[player_name] = score
pickle.dump(highscores, f)
print("The current high scores are as follows:")
print(highscores)
f.close()
def main():
trivia_file = open_file("trivia.txt", "r")
title = next_line(trivia_file)
welcome(title)
score = 0
#Get the first block
category, question, answers, correct, explanation, value = next_block(trivia_file)
while category:
#Ask a question
print(category)
print(question)
for i in range(4):
print("\t", i + 1, "-", answers[i])
#Get answer
answer = input("What is your answer?: ")
#Check answer
if answer == correct:
print("\nRight!", end=" ")
score += int(value)
else:
print("\nWrong!", end=" ")
print(explanation)
print("Score:", score, "\n\n")
#Get the next block
category, question, answers, correct, explanation, value = next_block(trivia_file)
trivia_file.close()
print("That was the last question!")
print("Your final score is", score)
return score
player_name = input("First, enter your name: ")
main()
saving(player_name)
input("\n\nPress the enter key to exit.")
此時出現同義錯誤:
def saving(player_name):
import pickle
f = open("trivia_scores.dat", "rb+")
highscores = pickle.load(f)
問題結束后,程序將嘗試運行“保存”模塊,(從理論上來說)該模塊將打開trivia_scores.dat文件,加載highscores詞典,檢查玩家的姓名是否在詞典中以及其當前分數高於文件中的那個,它將覆蓋它。
但是由於某種原因,當程序嘗試加載highscores字典時,卻收到了此錯誤消息。
EOFError: Ran out of input
我從未見過此錯誤。 從一些粗略的谷歌搜索中,我得到的印象是它與試圖從一個空文件中讀取的程序有關。 但這對我沒有意義,因為我專門使用其他程序創建了一個dat文件來防止這種情況的發生:trivia_scores.dat不是空文件。 我什至使用Python Shell進行閱讀以確保。
此錯誤是什么意思,為什么Python不會加載dat文件?
上下文:我正在閱讀的書是Michael Dawson撰寫的《 Python for Absolute Beginner》。 該程序和我要完成的挑戰來自第7章。在添加保存模塊之前,該程序運行良好。
您編寫的原始trivia_scores.dat
文件可能已損壞(也許您未在其上調用close()
?)。 您應該嘗試創建一個新文件,並向該文件添加一個預填充的字典。 然后嘗試從這個新文件中讀取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.