Reputation: 1
I'm making a trivia exercise in python 3.6.3 as part of an exercise at work. I have the whole script running successfully and triggering responses at the end correctly while tallying up the score correctly.It starts off like this:
#!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)
This repeats for 10 questions ending with: `#### Final score with if/elif parameters. percentage threshold is set up so I can add more questions down the line and I'm researching how to set some sort of recorded score function or top score board with user.
####
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')
What I would like to do, is record score/user and display the top 10 at the end and have it be persistent. I'm having a hard time finding a good source example of something like that so I'd appreciate any help.
Upvotes: 0
Views: 2315
Reputation: 2684
I suggest after the user finishes the quiz, to append their result to a file like so:
# 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()
Then read that file, sort by the scores, and print the result:
# 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
Upvotes: 1