Python can';t似乎从函数返回值,第二个函数将重新运行第一个函数
我有一个小程序,我需要修改,以增加高分存储能力。我已根据要求修改了代码,但出现了一个错误:Python can';t似乎从函数返回值,第二个函数将重新运行第一个函数,python,Python,我有一个小程序,我需要修改,以增加高分存储能力。我已根据要求修改了代码,但出现了一个错误: line 99, in high_scores entry = (score,name) NameError: name 'score' is not defined* 我的问题是:在函数high_scores中,如何获得我在main()函数中定义的变量scores?我已经在主函数中返回了变量分数,但我不知道如何在第二个函数中使用它,即high\u scores() 代码如下: # Trivia
line 99, in high_scores
entry = (score,name)
NameError: name 'score' is not defined*
我的问题是:在函数high_scores中,如何获得我在main()
函数中定义的变量scores
?我已经在主函数中返回了变量分数,但我不知道如何在第二个函数中使用它,即high\u scores()
代码如下:
# Trivia Challenge
# importing modules
import sys, pickle
def open_file(file_name, mode):
"""Open a file, specify a file_name, and mode)"""
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]
explanation = next_line(the_file)
points = next_line(the_file)
if points:
points = int(points[0])
return category, question, answers, correct, explanation, points
def welcome(title):
"""Welcome the player and get his/her name."""
print("\t\tWelcome to Trivia Challenge!\n")
print("\t\t", title, "\n")
def main():
trivia_file = open_file("trivia.txt", "r")
titlea = next_line(trivia_file)
welcome(titlea)
score = 0
int(score)
# get first block
category, question, answers, correct, explanation, points = next_block(trivia_file)
while category:
# ask a question
print(category)
print(question)
print("Points for this question: ", points)
for i in range(4):
print("\t", i+1, "=", answers[i])
answer = input("What's your answer?: ")
if answer == correct:
print("\nRight", end=" ")
score += points
else:
print("\nWrong.", end=" ")
print(explanation)
print("Score:", score, "\n\n")
# get next block
category,question,answers,correct,explanation,points = next_block(trivia_file)
trivia_file.close()
print("That was the last question!")
print("You're final score is", score)
if score > 0:
print("You have made the high scores list! Congratulations!)")
high_scores()
else:
print("You couldn't make the high scores list. Sorry")
return score # returning score so that i can call this function to use the variable in another function
def high_scores():
global name
try:
with open("high_scores.dat", "rb") as f:
scores = pickle.load(f)
except EOFError:
scores = []
entry = (score,name)
scores.append(entry)
f = open("high_scores.dat", "wb")
pickle.dump(scores, f)
f.close()
print("Your achievement has been successfully saved to the high scores list")
def display_scores():
"""display scores"""
try:
with open("high_scores.dat", "rb") as f:
scores = pickle.load(f)
print("\n\nName\t\tScore")
for entry in scores:
score, name = entry
print(name,"\t\t",score)
except EOFError:
print("Something unexpected occured...Ending the program")
input("\n\nPress the enter key to exit")
sys.exit()
response = None
while response != "1":
print("""
1. Exit
2. Play the game
3. Display high scores
"""
)
response = input("Enter your choice: ")
if response == "2":
name = input("please enter ur name: ")
main()
elif response == "3":
display_scores()
input("\n\nPress the enter key to exit.")
只需将其作为参数传递:
def high_scores(score):
...
...
def main():
...
high_scores(score)
只需将其作为参数传递:
def high_scores(score):
...
...
def main():
...
high_scores(score)
为什么不在
main
函数之外定义score
呢?为什么不在main
函数之外定义score
呢?我已经完全忘记了!你救了我!我花了大约两个小时的时间在这上面,但我仍然无法独自思考。多谢了,我完全忘了!你救了我!我花了大约两个小时的时间在这上面,但我仍然无法独自思考。非常感谢你。