请帮助我了解我在做什么错。
我出错了


  NameError:名称“名称”未定义


无论功能欢迎返回名字。
这是代码。

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 - 迈克尔·道森(Michael Dawson)的琐事游戏任务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36890019/

10-11 00:29