2016-04-18 16 views
-1

Ich arbeite gerade durch den Python für absolute Anfänger 3. Zusatz. Ich habe mit der 2. Herausforderung im 7. Kapitel des Buches zu kämpfen, da ich immer einen Fehler bekomme, den ich nicht verstehe.Python für absolute Anfänger Kapitel 7 Herausforderung 2

Die Herausforderung besteht darin:

„triva Herausforderung Spiel zu verbessern, so dass es eine Highscore-Liste in einer Datei verwaltet Das Programm sollte den Namen des Spielers aufzeichnet und sammelt, wenn der Spieler die Liste macht speichern Sie die Highscore.. ein gebeiztes Objekt verwenden. "

Der ursprüngliche Code

# 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.") 

an dem Challenge-Code meinen Versuch

# 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.") 

und die wunderbar verwirrend Fehler

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 

Kann mir bitte jemand erklären, was hier falsch läuft? Ich habe tagelang darauf gestarrt.

+0

Bitte teilen Sie uns mit, wie der Code verwendet wird. Was ist Ihre Hauptaufgabe? – harfel

+4

Fügen Sie nicht jede Version von Python, die jemals erstellt wurde, in Ihre Tags ein. Die Tags sollen Ihre Frage kategorisieren. Welche Version ist es wirklich? Verwenden Sie dieses Tag. Normalerweise reicht das allgemeine 'python-3.x' Tag. – zondo

+0

@ChatterOne Die Pickle-Datei ist dort ich habe überprüft und eine neue Datei erstellt und versucht, diese zum gleichen Ergebnis. –

Antwort

0

Sie haben eine „EOFError“, die eine „End Of File Error“ ist in Zeile 54

Das ist, wo Sie versuchen, so die Beize-Datei zu laden, wenn man bedenkt, dass Sie nicht überprüfen, ob die Datei tatsächlich existiert Ich vermute, dass Sie keine Datei haben und den Fehler erhalten.

Erstellen Sie entweder eine Initialdatei, oder prüfen Sie, ob sie existiert und gültig ist, bevor Sie versuchen, sie zu laden.

EDIT: Ich habe gerade bemerkt, dass Sie die Gurke-Datei als "wb +" öffnen, was bedeutet, dass Sie es zum Schreiben öffnen und versuchen, es zu lesen. Sie überschreiben die Datei, die zu Null Bytes wird. Wenn Sie an die vorhandene Datei anhängen möchten, sollten Sie "a" anstelle von "w" verwenden. Stellen Sie vor dem Laden sicher, dass die Datei gültige Daten enthält.

+0

müssten Sie nicht die Datei für 'rb' lesen oder' rb + 'lesen und optional schreiben? –