我正在python 3.6.3中进行琐事练习,作为工作练习的一部分。我已经成功运行了整个脚本并在正确计算得分时正确地触发了响应。它开始是这样的:
#!python3.6
## greet user, take name, explain rules ##
print ('HELLO! WELCOME TO MY MOVIE TRIVIA QUIZ. \n')
name = input('WHAT IS YOUR NAME?')
print ('\n WELCOME TO THE THUNDERDOME ' + name + '! SHALL WE PLAY A GAME? \n')
print ('I WILL ASK YOU 10 MOVIE TRIVIA QUESTIONS AND YOU SHALL HAVE 3 CHOICES FOR ANSWERS.\n \n PLEASE ANSWER IN A ABC FORMAT.\n \n CHOOSE WISELY, FOR YOU SHALL BE JUDGED\n')
print ('IMPORTANT DISCLAIMER FOR MORTALS : PLEASE KEEP YOUR CAPS LOCK ON')
print ('\n-----------------------------------------------------------\n')
##set the score at 0##
score = 0
score = int(score)
重复10个问题,结尾为:`#### if / elif参数的最终得分。设置了百分比阈值,这样我就可以添加更多问题,我正在研究如何设置某种记录得分函数或用户的最高得分板。
####
print ('Prepare to be judged: ' + str(score) + ' out of 10')
percentage = (score/10)*100
print ('The verdict is:', percentage)
if percentage < 20.0:
print ('I Dont normally judge people, but you need to see more movies and have no culture.')
elif percentage >=20.1 and percentage <=40.0:
print ('Nice, there is some hope for you.')
elif percentage >=40.1 and percentage <=60.0:
print ('Ohhhh, you fancy yourself a trivia buff? step correct next time.')
elif percentage >=60.1 and percentage <=80.0:
print ('Color me impressed! You are getting closer.')
elif percentage >=80.1 and percentage <=99.9:
print ('You are gonna break the game, you are on fire!.')
if percentage ==100.0:
print ('I HAVE VIEWED UPON YOUR MIND AND DEEMED THAT YOU ARE WORTHY OF GODHOOD')
print ('\n-----------------------------------------------------------\n')
我想做的是记录得分/用户,并在最后显示前十名并保持持久性。我很难找到类似的东西的好例子,所以我很感激任何帮助。
答案 0 :(得分:1)
我建议在用户完成测验后,将结果附加到如下文件中:
# Writing high scores to file
file = open('highscores.txt','a')
name='foo';score=78
file.write(name + " " + str(score) + "\n")
name='bobby';score=56
file.write(name + " " + str(score) + "\n")
file.close()
然后读取该文件,按分数排序,并打印结果:
# Reading/Printing the output
file = open('highscores.txt').readlines()
scores_tuples = []
for line in file:
name, score = line.split()[0], float(line.split()[1])
scores_tuples.append((name,score))
scores_tuples.sort(key=lambda t: t[1], reverse=True)
print("HIGHSCORES\n")
for i, (name, score) in enumerate(scores_tuples[:10]):
print("{}. Score:{} - Player:{}".format(i+1, score, name))
HIGHSCORES
1. Score:99.0 - Player:joe
2. Score:78.0 - Player:foo
3. Score:56.0 - Player:bobby