0
我有一個瑣事程序,需要修改才能添加高分存儲功能。我修改了代碼的要求,但我得到一個錯誤:似乎無法從函數返回值,第二個函數重新運行第一個函數
line 99, in high_scores
entry = (score,name)
NameError: name 'score' is not defined*
我的問題是:在功能high_scores,我怎樣才能變score
我在main()
函數定義?我已經在主函數中返回了變量分數,但是我不知道如何在我的第二個函數high_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.")
爲什麼不定義'score'的'main'功能之外? –