2016-04-18 30 views
-1

我目前正在通过python for绝对初学者第3版添加。在本书第七章中,我一直在努力应对第二个挑战,因为我不断收到一个我不明白的错误。用于绝对初学者的Python第7章挑战2

面临的挑战是:

“提高triva挑战游戏,使其保持在一个文件中的高分榜的应程序记录了玩家的名字,如果该玩家能够上榜,成绩存储高分。使用一个腌制的物体。“

原代码

# 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 trivia 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) 

    return category, question, answers, correct, explanation 

def welcome(title): 
    """Welcome the player and get his/her name.""" 
    print("\t\tWelcome to Trivia Challenge!\n") 
    print("\t\t", title, "\n") 

def main(): 
    trivia_file = open_file("trivia.txt", "r") 
    title = next_line(trivia_file) 
    welcome(title) 
    score = 0 

    # get first block 
    category, question, answers, correct, explanation = 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's your answer?: ") 

     # check answer 
     if answer == correct: 
      print("\nRight!", end=" ") 
      score += 1 
     else: 
      print("\nWrong.", end=" ") 
     print(explanation) 
     print("Score:", score, "\n\n") 

     # get next block 
     category, question, answers, correct, explanation = next_block(trivia_file) 

    trivia_file.close() 

    print("That was the last question!") 
    print("You're final score is", score) 

main() 
input("\n\nPress the enter key to exit.") 

我在尝试挑战码

# Trivia Challenge 
# Trivia game that reads a plain text file 

import sys, pickle 

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 trivia file.""" 
    category = next_line(the_file) 

    points = 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) 

    return category, points, question, answers, correct, explanation 

def welcome(title): 
    """Welcome the player and get his/her name.""" 
    print("\t\tWelcome to Trivia Challenge!\n") 
    print("\t\t", title, "\n") 

def high_scores(): 
    global score 
    value = int(score) 
    name = input("What is your name? ") 
    entry = (value, name) 
    f = open("high_scores.dat", "wb+") 
    high_scores = pickle.load(f) 
    high_scores.append(entry) 
    high_scores = high_scores[:5] 
    print("High Scores\n") 
    print("NAME\tSCORE") 
    for entry in high_scores: 
     value, name = entry 
     print(name, "\t", value) 
    pickle.dump(high_scores, f) 
    f.close() 

def main(): 
    trivia_file = open_file("trivia.txt", "r") 
    title = next_line(trivia_file) 
    welcome(title) 

    # get first block 
    category, points, question, answers, correct, explanation = 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's your answer?: ") 

     # check answer 
     if answer == correct: 
      print("\nRight!", end=" ") 
      j = int(points) 
      global score 
      score += j 
     else: 
      print("\nWrong.", end=" ") 
     print(explanation) 
     print("Score:", score, "\n\n") 

     # get next block 
     category, points, question, answers, correct, explanation = next_block(trivia_file) 

    trivia_file.close() 

    print("That was the last question!") 
    print("You're final score is", score) 
    high_scores() 

score = 0 

main() 
input("\n\nPress the enter key to exit.") 

而奇妙的令人困惑的错误

Traceback (most recent call last): 
    File "C:\Users\Cheyne\Desktop\Python\chapter07\Challenges\temp.py", line 104, in <module> 
    main() 
    File "C:\Users\Cheyne\Desktop\Python\chapter07\Challenges\temp.py", line 100, in main 
    high_scores() 
    File "C:\Users\Cheyne\Desktop\Python\chapter07\Challenges\temp.py", line 54, in high_scores 
    high_scores = pickle.load(f) 
    File "C:\Python31\lib\pickle.py", line 1365, in load 
    encoding=encoding, errors=errors).load() 
EOFError 

任何人都可以帮忙解释这里发生了什么问题吗?我一直盯着它好几天。

+0

请与我们分享如何使用代码。你的主要功能是什么? – harfel

+4

不要把每个版本的Python都制作成标签。这些标签旨在对您的问题进行分类。它真的是什么版本?使用该标签。通常,一般的'python-3.x'标签就足够了。 – zondo

+0

@ChatterOne pickle文件在那里,我已经检查并创建了一个新文件,并尝试了一个相同的结果。 –

回答

0

你有一个“的EOFError”,这是一个“结束文件错误”在线路54

这就是你尝试加载泡菜文件,这样,考虑到你是不是检查该文件确实存在,我的猜测是你没有文件,并得到错误。

要么自己创建一个初始文件,要么在尝试加载之前检查它是否存在并且是有效的。

编辑:我只是注意到,你打开泡菜文件为“wb +”,这意味着你打开它的写作,并尝试阅读它。您正在覆盖文件,该文件变为零字节。如果你想附加到现有的文件,你应该使用“a”而不是“w”。再次,加载之前确保文件包含有效的数据。

+0

难道你不需要打开文件来读取'rb'或'rb +'读取和可选的文字吗? –