Question
For the trivia game assignment I have almost the finishing touches on the outline but there is something that is still wrong. In the code
For the trivia game assignment I have almost the finishing touches on the outline but there is something that is still wrong. In the code I am trying to shuffle the correct answers so the user does not have to put only 1 in and gets all of them right. What is happening is the answers are shuffling and it is not changing it. I am not trying to have every correct answer be A, the final build will have the correct answer be different names or other indicators so I want to have the correct answer be indicated by it being the first integer. In the code it did this through if answers[int(user_answer)-1]: I would like to get that to shuffle and still lable what is incorrect and what is correct.
here is the code for reference
import random
def main(): # Define the categories, questions, and answers for the trivia game. categories = { "Category 1": { "Question 1, ": ["A", "Answer 2", "Answer 3", "Answer 4"], "Question 2": ["A", "Answer 2", "Answer 3", "Answer 4"] }, "Question 2": { "Question 1": ["A", "Answer 2", "Answer 3", "Answer 4"], "Question 2": ["A", "Answer 2", "Answer 3", "Answer 4"] }, "Category 3":{ "Question 1": ["A", "Answer 2", "Answer 3", "Answer 4"], "Question 2": ["A", "Answer 2", "Answer 3", "Answer 4"] }, "Category 4":{ "Question 1": ["A", "Answer 2", "Answer 3", "Answer 4"], "Question 2": ["A", "Answer 2", "Answer 3", "Answer 4"] } }
# Run the trivia game by looping through each category and question, displaying the question and answer choices, # and prompting the user for their answer. score = 0 total_questions = 0 for category in categories: print(f" {category}:") for question in categories[category]: total_questions += 1 print(f" {question}") answers = categories[category][question] random.shuffle(answers) for i in range(len(answers)): print(f"{i+1}. {answers[i]}") user_answer = input(" Enter your answer (1-4) or type 'exit' to quit: ") if user_answer == "exit": print("Thanks for playing!") return if user_answer.isdigit() and 1 <= int(user_answer) <= 8: if answers[int(user_answer)-1]: print("Correct!") score += 1 else: print("Incorrect.") else: print("Invalid input. Skipped question.")
# Display the user's score as a percentage and give a message based on the percentage. percentage = score / total_questions * 100 if percentage >= 90: message = "Nice job!" else: message = "Needs work." print(f" You scored {score} out of {total_questions} ({percentage:.2f}%). {message}")
if __name__ == "__main__": main()
I was suggested to put correct_answers_indext to be "A" but Like I stated I am going to have the answers be not just A when the final build is put in.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started