2017-04-24 6 views
1

Der Versuch, ein Spiel zu haben, wo jede Frage einen einzigartigen Wert zugeordnet hat. Die Punktzahl des Spielers ist dann die Gesamtzahl der Punkte der Fragen, die er oder sie richtig beantwortet. Wurde damit das Hantieren aber ich in diese Fehler nicht unterbrochen wird: enter image description herePython Trivia Spiel Fehler

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) 
    point_value = 0 
    question = next_line(the_file) 

    answers = [] 
    answers.append(next_line(the_file)) 

    if(answers[0]=="True\n"): 
     answers.append(next_line(the_file)) 
    else: 
     for i in range(4): 
      answers.append(next_line(the_file)) 

    correct = next_line(the_file) 
    if correct: 
     correct = correct[0] 
     point_value = (int)(next_line(the_file).strip()) 
    explanation = next_line(the_file) 

    return category, question, answers, correct, explanation, point_value 

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, point_value = next_block(trivia_file) 
    while category: 
     # ask a question 
     print(category) 
     print(question) 
     i=0 
     for a in answers: 
      print ("\t", i + 1, "-", a) 
      i = i + 1  # 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, score, point_value = 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.") 

nicht sicher, warum es diese Fehler mit/warum es nicht läuft - Vorschläge? ty!

Dies ist mit einer separaten .TXT-Datei namens "trivia.txt" mit allen Fragen und Punkten verbunden.

Antwort

2

Am wahrscheinlichsten tritt der Fehler auf, weil Ihre Textdatei Unicode-Zeichen enthält. Sie können den Parameter encoding zum Aufruf open hinzufügen, um Python mitzuteilen, dass er nicht in der Standardcodierung ascii ist.

the_file = open(file_name, mode, encoding='utf-8') 

Wenn dies nicht funktioniert, kann es sein, da die Datei eine andere Kodierung wie ‚iso-8859-15‘ verwendet. Die Python-Dokumentation Unicode-HOWTO enthält weitere Details zum Umgang mit Reading and Writing Unicode Data.