Question: My code, in Python, below is for a trivia game show (there are more questions, they're just long.) I need to create an updating score

My code, in Python, below is for a trivia game show (there are more questions, they're just long.) I need to create an updating score board but the dictionary that holds the scores does not update and I cannot figure out why. I am not supposed to create a separate file to hold the scores. The actual question is as follows: Add a View high score table to your main menu. The high score table only needs to exist for one execution of your module (i.e., scores dont have to be saved to disk). Users should be prompted for their name after they finish answering all of the questions. If the user attempts to view the high score table when it is empty, you must let them know it is empty. Otherwise, the high score table (including names and scores) should be output from highest to lowest (ties may be printed in any order). After viewing high scores, the main menu should appear. Code: def main():
 questions = [ {"question": "What's .5 + .5?", "answers": [ "1", "2", "3", "4"], "correct": "1"}]
# Provide introduction and then provide menu with options print("Welcome to our Trivia Game! ") print("Can you prove your knowledge or are you half baked? ") print("Main Menu " "1. Play Trivia " "2. View Credits " "3. View Score Board " "4. Exit Program ") choice = int(input("Select an option: ")) print("") while int(choice) not in range(1, 5): choice = input("That is not a menu option, try again: ") if choice == 1: play(questions) elif choice == 2: credit() elif choice == 3: score_board() elif choice == 4: exit # Function for playing the trivia game def play(questions): # Ask for name here to add to score board on completion name = input("Please enter player name: ") # Start score tally at zero and add one on each correct answer score = 0 total = 0 # Shuffle questions in dictionary before calling on them random.shuffle(questions) # Use for loop to ask each question for question in questions: print(question["question"]) for i, choice in enumerate(question["answers"]): print(str(i + 1) + ". " + choice) answer = input("Choose an answer: ") # Use while loop to identify any numbers not acceptable as an answer while int(answer)-1 not in range(len(question["answers"])): print(" That answer is not an option, please try again. ") answer = input(" Choose an answer: ") if answer == question["correct"]: score += 1 print(" That is correct! Good job. ") else: print(" Whoops, that's not right. ") total += 1 print("Your current score is", score, "out of", total, " ") # Report score to user print(name + ", you got", score, "out of 10 correct. ") # Add the score and user name to score board board(name, score) main() # Credits function that prints a statement on who made the program def credit(): print("Baking Trivia made by: " " Cierra Cuppett and Hannah Larreau " " With guidance from: " " NAU Faculty like you, Thank You. ") main() # Created because I was running out of ideas, this doesn't have to stay here. def board(name, score): sboard = {} sboard[name] = score print(sboard) # Function for displaing the board list def score_board(board): if len(board) == 0: print("SCORE BOARD EMPTY") else: # Sort board by descending so highest score is on first sorted_board = sorted(board.items(), key=operator.itemgetter(1), reverse=True) # Print name and score in sorted order for name, score in sorted_board: print(name, score+" ") main() main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!