迈克尔·道森的琐事游戏任务



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

名称

错误:未定义名称"名称"

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

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("nnPress 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("ttWelcome to Trivia Challenge!n")
    print("tt", 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, "nn")
        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("nnPress the enter key to exit.")

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

name = welcome(title)

最新更新