python 问答游戏错误

标签 python python-3.x function debugging

尝试制作一款游戏,其中每个问题都有与之相关的独特值(value)。玩家的分数就是她或他正确回答问题的总分。一直在摆弄它,但我不断遇到这些错误: enter image description here

代码:

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

不确定为什么会出现这些错误/为什么它没有运行 - 建议?蒂!

这连接到一个名为“trivia.txt”的单独 .txt 文件,其中包含所有问题和要点。

最佳答案

发生该错误很可能是因为您的文本文件包含 unicode 字符。您可以将 encoding 参数添加到 open 调用中,以告诉 python 它不是默认的 ascii 编码。

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

如果这不起作用,可能是因为该文件使用不同的编码,例如“iso-8859-15”。

Python 文档 Unicode-HOWTO有关处理 Reading and Writing Unicode Data 的更多详细信息.

关于python 问答游戏错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43579051/

相关文章:

python - 从文件中读取连续的三列到数组中

python - 如何根据示例数据框的 created_time 列的日期和时间条件获取记录

python - 从 HDF5 文件中序列化和检索大量 numpy 数组的快速有效的方法

python - isinstance 在 python 中如何为子类工作?

python - Jinja 2 - Django 表单 : rendering encodes HTML

python - 如何向networkx中的节点添加属性

python-3.x - “ float ”对象是不可切片的

r - 附加函数名称,但参数较少

python - 如何将函数发送到远程 Pyro 对象

python - range 和 len 函数的替代方案