import random # Added a maximum number for the amount of questions and answering the question # Check that users have entered a valid # option based on a list def string_checker(question, valid_ans): while True: error = f"Enter a valid option from {valid_ans}" user_response = input(question).lower() for item in valid_ans: # check if the user response is a valid answer if item == user_response: return item elif user_response == item[0]: return item print(error) print() def instructions(): print(''' **** Instructions **** To begin, choose the number of questions, 5 is the minimum and 10 is the maximum. If an invalid or a number outside of the minimum and maximum range is entered, a helpful hint will be displayed. If an incorrect answer is entered, a message will display the correct answer. There is an option at the end of the quiz to see the quiz history ''' ) # compare the correct answer v the user input # result - correct or incorrect def compare_ans(user, ans): if user == ans: round_result = "correct" else: round_result = "incorrect" return round_result # checks the input is an integer def int_check(question, low, high): while True: error = f"\U0001F4A1 Hint \U0001F4A1 Enter an integer more than {low} and below {high}" to_check = input(question) try: response = int(to_check) # check the number is equal or more if low <= response <= high: return response else: print(error) except ValueError: print(error) # Main routine # game variables rounds_played = 0 questions_incorrect = 0 questions_won = 0 yes_no = ["yes", "no"] quiz_history = [] print() print(" ⛰️ 🧻 ✂️ Multiplication Quiz ⛰️ 📰 ✂️ ") print() print("** Welcome to the Multiplication Quiz **") print() # instructions want_instructions = string_checker("Do you want to see the instructions? ", yes_no) if want_instructions == "yes": instructions() num_rounds = int_check("Enter the number of questions, 5 is the minimum, 10 is the maximum ", 5, 10) # game loop starts here while rounds_played < num_rounds: rounds_heading = f"\n*** Question {rounds_played + 1} of {num_rounds} ***" print(rounds_heading) # Generate random numbers num_1 = random.randint(5, 10) num_2 = random.randint(5, 10) # Generate correct answer correct_answer = num_1 * num_2 user_answer = int_check(f"What is {num_1} x {num_2}? ", 25, 100) # compare user answer result = compare_ans(user_answer, correct_answer) # Add counters for the result and the game history if result == "correct": questions_won += 1 feedback = "\U0001F607 \U0001F929 Correct Answer! \U0001F929 \U0001F607" else: questions_incorrect += 1 feedback = ("*** Incorrect! *** \t" f"The correct answer is {correct_answer}") history_item = f"Question: {rounds_played + 1} - {feedback}" print(feedback) quiz_history.append(history_item) rounds_played += 1 # Calculate stats rounds_won = rounds_played - questions_incorrect percentage_won = questions_won / rounds_played * 100 percentage_lost = questions_incorrect / rounds_played * 100 # Display Game Stats print() print("**** QUIZ STATS ****") print(f"You correctly answered {rounds_won} out of {rounds_played} questions") print(f"Won: {percentage_won:.2f} % \t" f"Lost: {percentage_lost:.2f} % \t") print() # Game history option see_history = string_checker("Do you want to see your quiz history? ", yes_no) if see_history == "yes": for item in quiz_history: print(item) # End of program message print() print("The quiz has ended")