import random # check that users have entered a valid option based on a list def string_checker(question, valid_ans=('yes', 'no')): error = f"Please enter a valid option from the following list: {valid_ans}" while True: # get user response and make sure it's lowercase user_response = input(question).lower() for item in valid_ans: # check if the user response is a word in the list if item == user_response: return item # check if the user response is the same as # the first letter of an item in the list elif user_response == item[0]: return item # print error if user does not enter something that is valid print(error) print() # Inter checker function to validate the user input is a number and is within a number range def int_check(question, low, high): error = f'Please enter a number between {low} and {high}.' while True: try: response = int(input(question)) if response >= low and response <= high: return response else: print(error) except ValueError: print(error) # Variables difficulty_list = ["easy", "medium", "hard"] easy_difficulty = ["+", "-"] medium_difficulty = ["x", "÷", "+", "-"] hard_difficulty = ["x", "÷"] game_history = [] rounds_played = 0 rounds_won = 0 rounds_lost = 0 print() print("Maths Quiz") print() # asks user for number of rounds they want rounds = int_check(f"How many rounds do you want? (3 to 10): ", 3, 10) print(f"Great you chose {rounds} rounds.") print() # asks user for what difficulty they want rounds_difficulty = string_checker("What difficulty do you want? (Easy (e), Medium (m), or Hard (h): ", difficulty_list) print(f"You chose {rounds_difficulty} mode") print() # questions for the user starts here and loops until 'round_played' match 'rounds' # in other words, keeps repeating until the users chosen number of rounds are done while rounds_played < rounds: # picks 'easy', 'medium' or 'hard' based on user input # then chooses what the numbers are and then if its '+', '-', or 'x' # based on the difficulty if rounds_difficulty == "easy": num1 = random.randint(1, 10) num2 = random.randint(1, 10) round_variable = random.choice(easy_difficulty) if round_variable == "-": if num1 > num2: answer = num1 - num2 rounded_num = answer else: answer = num2 - num1 rounded_num = answer else: answer = num1 + num2 rounded_num = answer elif rounds_difficulty == "medium": num1 = random.randint(4, 12) num2 = random.randint(4, 12) round_variable = random.choice(medium_difficulty) if round_variable == "x": answer = num1 * num2 rounded_num = answer elif round_variable == "-": if num1 > num2: answer = num1 - num2 rounded_num = answer else: answer = num2 - num1 rounded_num = answer elif round_variable == "+": answer = num1 + num2 rounded_num = answer else: num1 = random.randint(3, 12) * 2 num2 = random.randint(1, 3) * 2 answer = num1 / num2 rounded_num = round(answer, 2) else: num1 = random.randint(6, 15) num2 = random.randint(6, 15) round_variable = random.choice(hard_difficulty) if round_variable == "x": answer = num1 * num2 rounded_num = answer else: num1 = random.randint(3, 7) * 2 num2 = random.randint(1, 3) * 2 answer = num1 / num2 rounded_num = round(answer, 2) # asks user question with the high number displayed first and low number last if num1 > num2: print(f"Round {rounds_played + 1} of {rounds} rounds") user_answer = int_check(f"What is {num1} {round_variable} {num2}? ", 0, 500) else: print(f"Round {rounds_played + 1} of {rounds} rounds") user_answer = int_check(f"What is {num2} {round_variable} {num1}? ", 0, 500) if rounded_num == user_answer: rounds_won += 1 result_display = "✅ Woohoo! Correct answer ✅" print(result_display) print() else: rounds_lost += 1 result_display = f"❌ Incorrect. The correct answer was {rounded_num} ❌" print(result_display) print() if num1 > num2: history_item = f"Round {rounds_played + 1}: {num1} {round_variable} {num2} = {user_answer} - {result_display}" else: history_item = f"Round {rounds_played + 1}: {num2} {round_variable} {num1} = {user_answer} - {result_display}" game_history.append(history_item) rounds_played += 1 if rounds_played > 0: # calculate statistics percent_won = rounds_won / rounds_played * 100 percent_lost = rounds_lost / rounds_played * 100 # output game Statistics print("Game Statistics") print(f"Won: {percent_won:.2f}%\t " f"Lost: {percent_lost:.2f}%\t ") # ask user if they want to see their game history and output if requested. see_history = string_checker("\nDo you want to see your game history? (yes / no): ") if see_history == "yes": for item in game_history: print(item) # end of program print() print("Thanks for playing!") print("The program has ended")