# I have removed the mode checker, as it was not required when the string checker was present. # I changed comments to align with their function in the context of the maths quiz # I have simplified the instructions function # I added a percentage to thr rounds won / lost so that the user understands what the displayed values mean. # I added emojis to certain outputs. # I have changed the If/else function (for mode_choice) to make the program more flexible, by reducing hard # coding (using round_list instead of names such as easy). import random # Check the user enters a valid # option based on a list def string_checker(question, valid_ans=("yes", "no")): error = f"Please enter a option from the following list: {valid_ans}" while True: # Get user response and make sure it is lowercase user_response = input(question).lower() for item in valid_ans: # check if the users response is a word in the list if item == user_response: return item # check if the users 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 a valid response print(error) print() # Checks that the user enters an integer more than / equal to the lowest possible value, # and greater than / equal to the highest possible value. def int_check(question, low, high): while True: error = f"please input an integer (whole number) between or equal to {low} and {high}." try: response = int(input(question)) # checks that their number is more than / equal to the low and greater than / equal to the high value if response > high or response < low: print(error) else: return response except ValueError: print(error) # Compare user choice and answer and return # Result (correct / incorrect) def ans_compare(user, answer): # If the users answer and actual answer are the same, the user is correct. if user == answer: round_result = 'Correct' # If the answers don't match, the user is incorrect else: round_result = "Incorrect" return round_result # Initialise game variables mode_list = ["easy", "medium", "hard"] rounds_played = 0 rounds_lost = 0 rounds_won = 0 game_history = [] print(" Welcome to the Mathematics game") print() def instructions(): print() print(""" ****Instructions**** To begin, choose the number of rounds you would like to play, and one of following modes: E(Easy)😁, M(Medium)😐, and H(hard)😡. Each individual mode difficulty corresponds with the following mathematical concepts: o Easy is addition (➕) o Medium is multiplication (✖) o Hard is squaring numbers (⁕2) Then, enter the number of rounds you would like to play (1 minimum and 15 maximum) Then, you will be given a question to answer. A impossible value (for the set questions) will result in a retry (re-enter input) Good luck! """) # Ask the user if they want to read the instructions (check if they want to see the instructions (yes / no)) want_instructions = string_checker(f'Would you like to see the instructions?').lower() # Display the instructions if the user agreed to seeing them... if want_instructions == "yes": instructions() # Ask the user which mode they would like to play (Easy(E), Medium(M), or hard(H)) mode_choice = string_checker(f'Which mode would you like to play?', mode_list) if mode_choice == mode_list[0]: print("You will answer addition problems.") elif mode_choice == mode_list[1]: print("You will answer multiplication problems.") else: print("you will answer squaring problems.") # Ask for the number of rounds the user would like to play num_rounds = int_check("How many rounds would you like to play? Please choose", 1, 15) # Game loop starts here while rounds_played < num_rounds: # rounds headings rounds_heading = f"\n Round {rounds_played + 1} of {num_rounds}" print(rounds_heading) # Randomly choose two numbers (from and including 1 to 10), and add / multiply / # square them (depends on difficulty) to create # a solution to a random question. num1 = random.randint(1, 10) num2 = random.randint(1, 10) ans_easy = num1 + num2 ans_medium = num1 * num2 ans_hard = num1 ** 2 # gets user choice if mode_choice == mode_list[0]: user_input = int_check(f"What does {num1} + {num2} equal? ", 2, 20) result = ans_compare(user_input, ans_easy) actual_ans = ans_easy elif mode_choice == mode_list[1]: user_input = int_check(f"What does {num1} x {num2} equal? ", 1, 100) result = ans_compare(user_input, ans_medium) actual_ans = ans_medium else: user_input = int_check(f"What does {num1} squared equal? ", 1, 100) result = ans_compare(user_input, ans_hard) actual_ans = ans_hard # Adjust game lost / won and adds results to the game history if result == "Correct": rounds_won += 1 feedback = "Correct ✅" else: rounds_lost += 1 feedback = f"Incorrect ❌, the answer was {actual_ans} " # set up round feedback and output it to the user # add it to the game history list (includes the round number) round_feedback = f"{feedback}" history_item = f"Round: {rounds_played + 1} - {round_feedback}" print(round_feedback) game_history.append(history_item) rounds_played += 1 # game loop ends here # game history / statistics area # calculate statistics rounds_won = rounds_played - rounds_lost 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?") if see_history == "yes": for thing in game_history: print(thing) print() print("Thank you for playing")