2016-04-18 74 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 +'讀取和可選的文字嗎? –