gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:59:20 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com