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, num_type): while True: error = f'Please enter a number between {low} and {high}.' if num_type == "decimal": data_type = float else: data_type = int try: response = data_type(input(question)) if response >= low and response <= high: return response else: print(error) except ValueError: print(error) def instructions(): print(''' *** Instructions **** To begin, choose the number of rounds (3 to 10). Then choose what difficulty you want (Easy, Medium, or Hard). Then play and try at answer the questions: - Addition (+) - Subtraction (-) - Multiplication (+) - Division (÷) At the end, You may play again. If you chose to end the game, provided you have played two or more games, you will have your overall game stats. Good Luck! ''') # Variables outside while loop replay = "yes" games_won = 0 games_lost = 0 games_played = 0 game_rounds_played = 0 game_played_num = 0 difficulty_list = ["easy", "medium", "hard"] easy_difficulty = ["+", "-"] medium_difficulty = ["x", "÷", "+", "-"] hard_difficulty = ["x", "÷"] game_stats = [] print() print("Maths Quiz") print() # Asks user at the end if they want to play again # If then do, the replay starts here # ask user if they want to see the instructions and display # them if requested want_instructions = string_checker("Do you want to read the instructions? ") # checks users enter yes (y) or no (n) if want_instructions == "yes": instructions() while replay == "yes": # Variables game_history = [] rounds_played = 0 rounds_won = 0 rounds_lost = 0 # Asks user for number of rounds they want print() rounds = int_check(f"How many rounds do you want? (3 to 10): ", 3, 10, "integer") 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 '+', '-', 'x' or '/' # 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}") user_answer = int_check(f"What is {num1} {round_variable} {num2}? ", 0, 500, "decimal") else: print(f"Round {rounds_played + 1} of {rounds}") user_answer = int_check(f"What is {num2} {round_variable} {num1}? ", 0, 500, "decimal") if rounded_num == user_answer: rounds_won += 1 games_won += 1 result_display = "✅ Woohoo! Correct answer ✅" print(result_display) print() else: rounds_lost += 1 games_lost += 1 result_display = f"❌ Incorrect. The correct answer was {rounded_num} ❌" print(result_display) print() # Stores game history for later 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 game_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 game_played_num += 1 game_statistics = f"Game {game_played_num} Statistics - Won: {percent_won:.2f}%\t Lost: {percent_lost:.2f}%\t" print(game_statistics) game_stats.append(game_statistics) # 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) # Clears 'game_history' if the user wants to play again # This is so the history from the previous game isn't mixed in game_history.clear() # If user has play 2 or more games and chooses to end the program # Then the percent won and lost in every game will be displayed games_played += 1 # Asks user if they want to play again print() replay = string_checker("Do You want to play again? (yes / no): ") # 'Replay' while loop ends here if replay == "no": if games_played > 1: see_game_stats = string_checker("\nDo you want to see your game statistics? (yes / no): ") if see_game_stats == "yes": for item in game_stats: print(item) # Calculate games statistics if see_game_stats == "yes": game_percent_won = games_won / game_rounds_played * 100 game_percent_lost = games_lost / game_rounds_played * 100 # Output overall game Statistics print() print(f"Overall Game Statistics - Won: {game_percent_won:.2f}%\t Lost: {game_percent_lost:.2f}%\t ") # End of program print() print() print("Thanks for playing!") print("The program has ended")