python - 迈克尔·道森的问答游戏作业

标签 python python-3.x

请帮助我理解我做错了什么。 我收到错误

NameError: name 'name' is not defined

无论函数welcome返回名称。 这是代码。

import sys, pickle, shelve

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]

    score = next_line(the_file)
    explanation = next_line(the_file) 

    return category, question, answers, correct, score, explanation

def welcome(title):
    """Welcome the player and get his/her name."""
    print("\t\tWelcome to Trivia Challenge!\n")
    print("\t\t", title, "\n")
    name = input('Enter your name')
    return name

def pic_save(name, final_score):
    records = {name: final_score}
    f = open('pickles.dat', 'ab')
    pickle.dump(records, f)
    return records

def pic_read():
    f = open("pickles.dat", "rb")
    records = pickle.load(f)
    print(records)
    f.close()

def main():
    trivia_file = open_file("7.1.txt", "r")
    title = next_line(trivia_file)
    welcome(title)
    final_score = 0

    # get first block
    category, question, answers, correct, score, 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=" ")
            final_score += int(score)
            print("Score:", score, "\n\n")
        else:
            print("\nWrong.", end=" ")

        print(explanation)


        # get next block
        category, question, answers, correct, score, explanation = next_block(trivia_file)

    trivia_file.close()
    pic_save(name, final_score)
    print("That was the last question!")
    print("You're final score is", final_score)
    pic_read()

main()  
input("\n\nPress the enter key to exit.")

最佳答案

您可能想要做的是将 welcome 的返回值分配给名为 name 的变量,否则返回值将丢失。

name = welcome(title)

关于python - 迈克尔·道森的问答游戏作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36890019/

相关文章:

python - 如何根据模型领域选择形式

python - 使用正则表达式 python 迭代数据框

python - 在 python http.client.HTTPSConnection 中设置更新的 ssl 版本

python - 如何每半秒对 pandas 数据帧重新采样一次并每秒获取数据?

python - 关于filter和lambda的问题

python-3.x - 是否有一个 Python 库可以在提供邮政编码时返回一个城市?

python - 从字符串动态创建文件夹树

python: += s, 中的逗号有什么作用?

python - 调用没有参数的python类方法

python - pylint 只显示 VSCode 中的错误