I am trying to print out high scores from a quiz, from a txt file, and align the correct username with the correct score. I am also trying to print the scores from highest to lowest. Currently the scores are highest to lowest, but I haven't been able to keep the scores with the names. I have a brief understanding of dictionaries but I haven't learned them yet as I am new to coding. I cannot find an answer to my question on here and am new to the website so I am posting the whole code as I do not know what you will need and what you don't need.
I am not opposed to massively re-writing this, but I would like tom change as little as possible.
P.S:
Sorry for any weird formatting, copy paste didn't work as intended
#IMPORTS
import random
import time
#################TO DO##################
#Add 'Days since written' thing
################## TO ADD TO PLAN #################
#How I choose what questions to ask
#How I make sure I do not repeat questions
#Keeping track of score
#global highscore
def main(): #Main question asking function
global score
loop_text = 0 #Used for print formatting
while True:
if loop_text == 0: #Used for print formatting
go_ahead = input('\nAre you ready to continue? Y or N? ')
print('---------------------------------------')
if go_ahead in ["Y", "y", "Yes", "yes", "YES"]: #This checks for all logical ways they would answer yes
break
elif go_ahead in ["N", "n", "no", "No", "NO"]: #This checks for all logical ways they would answer no
loop_text = 1 #Used for print formatting
print('Oh... ok')
time.sleep(1) #Delay running the next line for one second
print('...')
time.sleep(1)
print('...')
time.sleep(1)
print('...')
time.sleep(1)
else:
print('Yes or no only please!')
else:
go_ahead = input('\nNow are you ready to continue? Y or N? ') #Used for print formatting
print('-----------------------------------------')
if go_ahead in ["Y", "y", "Yes", "yes", "YES"]: #This checks for all logical ways they would answer yes
break
elif go_ahead in ["N", "n", "no", "No", "NO"]: #This checks for all logical ways they would answer no
loop_text = 1
print('Oh... ok')
time.sleep(1)
print('...')
time.sleep(1)
print('...')
time.sleep(1)
print('...')
time.sleep(1)
else:
print('Yes or no only please!')
score = 0 #Setting score
error_count = 0 #GOTTA CHECK FOR THOSE ERRORS
#Questions
questions = ['\nWhat was the first video game ever made?\n',
'\nWhat was the most expensive video game to ever be developed?\n',
'\nWhat amount of time a day does the average gamer (In the US, age 13 or older) spend gaming?\n',
'\nWho is the founder of Nintendo?\n',
'\nWhat was the most purchased game of 2017?\n',
'\nWhen was E3 [Electronic Entertainment Expo] 2017?\n', '\nWhat was the most popular console of 2010?\n',
'\nWho was the most subscribed gaming youtuber of 2012?\n',
'\nWho won the Game of The Year award 2016?\n',
'\nWhen did DOOM release?\n']
#Options in the same pisitions as the questions so the program can print the right options easy
options = ['a) God of War 9\nb) Pacman \nc) Pong \nd) Tennis for Two',
'a) Destiny\nb) Call of Duty: Modern Warfare 2 \nc) Grand Theft Auto: V \nd) Disney Infinity',
'a) 54 minutes\nb) 25 hours\nc) 2 hours\nd) 30 minutes\n',
'a) Fusajiro Yamauchi\nb) Bob Dylan\nc) Steve Bovaird\nd) Nihonjin no Shimei',
'a) Jesus sim\nb) Farming Simulator 2017\nc) Call of Duty: WWII\nd) Destiny 2',
'a) 13 Jun 2017 - 15 Jun 2017\nb) 13 Jun 2017 - 14 Jun 2017\nc) 15 July 2017 - 13 July 2017\nd) 10 Jun 2017 - 18 Jun 2017)',
'a) Xbox 360\nb) PlayStation 3\nc) Xbox 1\nd) PlayStation 4',
'a) PrettyL8r\nb) Pewdiepie\nc) Greg\nd) NotGreg',
'a) Overwatch\nb) Treyarch\nc) Blizzard\nd) Rainbow Six Siege',
'a) December 10, 1993\nb) February 23, 1995\nc) January 32, 20019\nd) Yesterday']
#Answers in the same pisitions as the questions so the program easy
answers = ['d', 'b', 'a', 'a', 'c', 'a', 'a', 'b', 'c', 'a']
for i in range(len(questions)): #Repeating for how many questions there are
q_ask = random.randint(0,len(questions)-1) #Asigning a variable to a random variable equal to the length of the list -1 because of how piition 1 is actually pisition 0
while True:
if error_count > 0:
time.sleep(1) #Printing question with a delay because they tried to answer this question with an invalid response
print (questions[q_ask])
time.sleep(1)
else:
print (questions[q_ask]) #Printing question
print (options[q_ask]) #Printing options
user_answer = input('What is your answer?: ') #What is your answer?
user_answer = user_answer.lower() #This makes it easier to make decisions based on their input
if user_answer in ['a', 'b', 'c', 'd']:
if user_answer == answers[q_ask]: #This will run if they input a valid reponse
print('Good job! You are correct.')
questions.pop(q_ask) #Popping the question from the list of questions. Popping works without bugs whereas .remove didn't
options.pop(q_ask) #Popping the options from the list of options.
answers.pop(q_ask) #Popping the answer from the list of answers.
score += 1 #+1 Score!
error_count = 0
break #Next question
else:
print ('Unfortunately that is not correct! Better luck next time.') #They have not answered correctly
questions.pop(q_ask)
options.pop(q_ask)
answers.pop(q_ask)
error_count = 0
break #Next question
else:
time.sleep(1)
print ('ERROR, please input a, b, c or d only plesae!') #This is to tell them that they input and invalid option
time.sleep(1)
error_count += 1
print('\nYour score is >>>', score)
def high_scores(): #Highscores function
top_5_name = []
top_5_int = []
repeat_n = 0
repeat_i = 0
i = 0
print('\nScore : Name\n')
template = """{score} : {name}"""
display_good = template.format(score=score,name=user_name)
highscore_a = open("highscoreFile.txt",'a') #The file that keeps the highest scores of all time
highscore_a.write(display_good)
highscore_a.write('\n')
highscore_a.close()
highscore_r = open("highscoreFile.txt", "r")
file_open = open('highscoreFile.txt', 'r') # op
score_list=[line.rstrip('\n') for line in file_open]
file_open.close()
for i in score_list:
split_top_5 = score_list[repeat_i].split(":")
if len(top_5_int) < 5:
top_5_int.append(int(split_top_5[0]))
top_5_name.append(split_top_5[1])
elif int(split_top_5[0]) < int(top_5_int[0]):
top_5_int.append(int(split_top_5[0]))
top_5_name.append(split_top_5[1])
elif int(split_top_5[0]) < int(top_5_int[1]):
top_5_int.append(int(split_top_5[0]))
top_5_name.append(split_top_5[1])
elif int(split_top_5[0]) < int(top_5_int[2]):
top_5_int.append(int(split_top_5[0]))
top_5_name.append(split_top_5[1])
elif int(split_top_5[0]) < int(top_5_int[3]):
top_5_int.append(int(split_top_5[0]))
top_5_name.append(split_top_5[1])
elif int(split_top_5[0]) < int(top_5_int[4]):
top_5_int.append(int(split_top_5[0]))
else:
pass
if len(top_5_int) == 6:
top_5_int.sort(reverse=True)
top_5_int.pop(5)
else:
pass
repeat_i += 1
for i in score_list:
split_top_5 = score_list[repeat_n].split(":")
top_5_name.append(split_top_5[1])
repeat_n += 1
repeat_i = (top_5_int[0:5])
repeat_n = (top_5_name[0:5])
i = 0
num = 0
while True:
if num < 5:
try:
print(repeat_n[num], ':', repeat_i[num])
i += 1
num += 1
except IndexError:
pass
else:
break
#for i in score_list:
#split_top_5 = score_list[repeat].split(":")
#top_5_name.append(split_top_5[1])
#repeat += 1
#top_5_name.sort(reverse=True)
#repeat = (top_5_name[0:5])
#for i in repeat:
#print(i)
while True:
user_name = input('\nHello! What is your name?: ') #The users inputs their name
if len(user_name) == 0:
print('\nError, nothing was entered.') #This will run if the user enters nothing
else:
break
print('\nHello ', user_name.capitalize(), ', welcome to The Videogame Quiz!', sep='') #Welcome message
print('\nIn this quiz, you will be asked 10 questions about videogames.\nTry your best to answer all 10 correctly. Enter a, b, c or d,\ndepending on which answer you decide is right.') #Telling them how to do the quiz
while True:
main() #Main program
while True: #Loop to here when the program uses continue
go_again = input('\nDo you want to take the quiz again? Y or N: ') #Ask the user whether the user wants to take the quiz again or not
if go_again in ["Y", "y", "Yes", "yes", "YES", "N", "n", "no", "No", "NO", "nO", "YeS", "yES", "yEs", "YEs", "yeS", 'CLEAR']: #This checks for all logical ways they would answer no
break #break out of while True Loop
else:
print ('ERROR, please input Y or N') #Incorrect input
if go_again in ["Y", "y", "Yes", "yes", "YES", "YeS", "yES", "yEs", "YEs", "yeS"]: #Do they want to go again
#They want to go
continue #Loop to the while True
elif go_again == 'CLEAR':
wipe_file = open("highscoreFile.txt",'w')
wipe_file.close
break
else:
#They do not want to go again
high_scores() #Running the highscores function
print ('\nGoodbye!') #Goodbye!
break #Break out of while True Loop**strong text**