import random # Checks that a user input is a valid number def int_check(question, low, high, error_type): while True: # changes error message based on error_type if error_type == 1: error = f"Please enter a number between {low} and {high}" else: error = 'Please enter a number' to_check = input(question) try: response = int(to_check) # checks that the number is between low and high if low <= response <= high: return response else: print(error) except ValueError: try: if error_type == 1: response = int(to_check) # checks that the number is between low and high if low <= response <= high: return response else: print(error) else: response = float(to_check) # checks that the number is between low and high if low <= response <= high: return response else: print(error) except ValueError: print(error) # Check that users have entered a valid # option based on a list def string_checker(question, valid_ans): error = f"Please enter a valid option from the following list: {valid_ans}" while True: # Get user response and make sure its 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 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() #displays instructions if the user inputs yes def instructions(): """Prints instructions""" print(""" *** Instructions **** To begin, choose the number of rounds. Then, answer the given question with a number. Round to two decimal places when necessary. Good Luck! """) # Main routine goes here # Initialise game variables rounds_played = 0 operator_list = ["+", "-", "*", "/"] game_history = [] yes_no = ['yes', 'no'] print(f"\n✖️➕➗➖ Mathematics Quiz ➖➗➕✖️") print() # Ask the user if they want instructions (yes/no) want_instructions = string_checker("Do you want to see the instructions? ", yes_no) # Display the instructions if the user wants to see them... if want_instructions == "yes": instructions() # Ask the user for the number of rounds num_rounds = int_check("How many rounds (between 5 and 15) would you like: ", 5, 15, 1) # Game loop starts here while rounds_played < num_rounds: rounds_heading = f"\n⚙️⚙️⚙️ Round {rounds_played + 1} of {num_rounds} ⚙️⚙️⚙️" print(rounds_heading) # Deciding and generating question operator = random.choice(operator_list) num1 = random.randint(-12, 12) num2 = random.randint(-12, 12) #make sure there's no 0 when dividing if num1 == 0 and operator == '/': num1 = random.randint(1, 12) elif num2 == 0 and operator == '/': num2 = random.randint(1, 12) question = str(num1) + ' ' + operator + ' ' + str(num2) + ' ' + '=' + ' ' if operator == '/': correct_answer = float(round(eval(str(num1) + ' ' + operator + ' ' + str(num2)), 2)) else: correct_answer = int(eval(str(num1) + ' ' + operator + ' ' + str(num2))) # Get user input user_input = int_check(question, float('-inf'), float('inf'), 0) # if user input is 'xxx', end the game if user_input == 'xxx': break if user_input == correct_answer: feedback = '✅ Correct! ✅' print(feedback) else: feedback = '❌ Incorrect! ❌' print(f'{feedback} The correct answer was: {correct_answer}') # Set up round feedback and output it user. # Add it to the game history list (include the round number) round_feedback = f"You put: {user_input}, {feedback}" history_item = f"Round: {rounds_played + 1} - {round_feedback}" game_history.append(history_item) rounds_played += 1 # Game loop ends here # Game history if rounds_played > 0: 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) print() print("Thanks for playing.")