简体   繁体   English

python trivia游戏错误

[英]python trivia game error

Trying to have a game where each question has a unique value associated to it. 尝试制作一个游戏,其中每个问题都有与之相关的唯一值。 The player's score is then the total number of points of the questions she or he answers correctly. 那么,玩家的分数就是她或他正确回答的问题的总分。 Been fiddling with it but I keep running into these errors : 一直在摆弄它,但我一直遇到这些错误: 在此处输入图片说明

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

not sure why it's having these errors/why its not running - suggestions? 不知道为什么会有这些错误/为什么不运行-建议? ty! TY!

this is connected to a seperate .txt file named "trivia.txt" with all the questions and points. 该文件将连接到名为“ trivia.txt”的单独的.txt文件,其中包含所有问题和要点。

Most likely the error is occurring because your text file contains unicode characters. 因为您的文本文件包含Unicode字符,所以很可能发生错误。 You can add the encoding parameter to the open call to tell python that it isn't in the default ascii encoding. 您可以将encoding参数添加到open调用中,以告诉python它不是默认的ascii编码。

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

If this doesn't work, it may be because the file is using a different encoding such as 'iso-8859-15'. 如果这不起作用,则可能是因为文件使用了不同的编码,例如'iso-8859-15'。

The Python documentation Unicode-HOWTO has more details about dealing with Reading and Writing Unicode Data . Python文档Unicode-HOWTO具有有关处理读写Unicode数据的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM