import random # V3 Added a maximum number for the amount of questions and answering the question # V4 Added a level of difficulty, updated the int_check function to use min & max range # 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, enter the difficulty level between 1-3. Enter 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 between {low} and {high}" to_check = input(question) try: response = int(to_check) # check the number is equal to or between the min & max range if low <= response <= high: return response else: print(error) except ValueError: print(error) # Main routine # quiz 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() # Difficulty level input difficulty = int_check("Enter the difficulty level (1-easy, 2-medium, 3-hard): ", 1, 3) # Define range based on difficulty if difficulty == 1: min_num, max_num = 1, 5 level = "Easy" elif difficulty == 2: min_num, max_num = 5, 10 level = "Medium" else: min_num, max_num = 8, 12 level = "Hard" 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*** {level} Question {rounds_played + 1} of {num_rounds} ***" print(rounds_heading) # Generate random numbers and the min & max range num_1 = random.randint(min_num, max_num) num_2 = random.randint(min_num, max_num) min_range = min_num * min_num max_range = max_num * max_num # Generate correct answer correct_answer = num_1 * num_2 user_answer = int_check(f"What is {num_1} x {num_2}? ", min_range, max_range) # 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 = f"*** Incorrect! *** 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(f"**** {level} QUIZ STATS ****") print(f"You correctly answered {rounds_won} out of {rounds_played} questions") print(f"Correct: {percentage_won:.2f} % Incorrect: {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")