迈克尔道森的琐事游戏分配

2024-06-11 09:57:39 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮助我明白我做错了什么。 我弄错了

NameError: name 'name' is not defined

不管函数欢迎都返回名称。 这是密码。在

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

Tags: thenamedeflineopenblockfilenext