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"] rounds_played = 0 rounds_won = 0 rounds_lost = 0 # program showed to the user starts here print() print("Maths quiz") print() rounds = int_check(f"How many rounds do you want? (Put in number from 3 to 10) ", 3, 10) print(f"Great you chose {rounds} rounds.") print() # asks user for what difficulty they want rounds_diff = string_checker("What difficulty do you want? (Easy (e), Medium (m), or Hard (h): ", difficulty_list) print(f"You chose {rounds_diff}") 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: if rounds_diff == "easy": low_number = 1 high_number = 10 elif rounds_diff == "medium": low_number = 4 high_number = 12 else: low_number = 6 high_number = 15 low_number_range = low_number * low_number high_number_range = high_number * high_number num1 = random.randint(low_number, high_number) num2 = random.randint(low_number, high_number) low_number = num1 answer = num1 * num2 # tells user what round they are on out of the rounds they picked # then asks user question print(f"Round {rounds_played + 1} of {rounds} rounds") user_answer = int_check(f"What is {num1} x {num2}? ", low_number_range, high_number_range) if answer == 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 {answer} " print(result_display) print() 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 Statistices") print(f"Won: {percent_won:.2f}%\t " f"Lost: {percent_lost:.2f}%\t ") # end of program print() print("Thanks for playing!") print("The program has ended")