import random # Check that users have entered a valid answer option based on 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 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 entre something that is valid print(error) print() # Display instructions def instructions(): print(""" *** Instructions **** To begin, choose the number of rounds (you can play between 5 and 20 rounds) Then, chose your difficulty level from easy (questions between 1 and 5), medium (questions between 3 and 12) or hard (questions between 10 and 20). Finally, answer the multiplication question. Give it your best go! Your game statistics will be published at the end of the game. Good Luck! """) # Check for an integer between high and low def int_checker(question, low, high): while True: error = f"๐Ÿ’กPlease enter an integer that is between {low} and {high}." to_check = input(question) try: response = int(to_check) # checks that the number is equal or more if low <= response <= high: return response else: print(error) except ValueError: print(error) # Main Routine Starts here # Display game heading print("\n๐Ÿ”ขโœ๏ธโœ–๏ธ Welcome to the multiplication quiz! โœ–๏ธโœ๏ธ๐Ÿ”ข") print() # Establish lists yes_no = ["yes", "no"] difficulty_levels = ["easy", "medium", "hard"] # Ask user if they want to see the instructions want_instructions = string_checker("Do you want to see the instructions? ", yes_no) # Check users enters yes (y) or no (n) and display them if requested if want_instructions == "yes": instructions() while True: # Initialise game variables rounds_played = 0 answers_correct = 0 answers_incorrect = 0 percentage_correct = 0 game_history = [] # Ask user for a number of rounds between range (5-20) num_rounds = int_checker("How many rounds would you like to play (between 5 and 20)? ", 5, 20) # Ask user for difficulty level difficulty = string_checker("Choose a difficulty level from easy, medium, or hard: ", difficulty_levels) print(f"You chose, {difficulty}") # Game loop starts here while rounds_played < num_rounds: # Set number ranges based on chosen difficulty if difficulty == "easy": first_number = random.randint(1, 5) second_number = random.randint(1, 5) elif difficulty == "medium": first_number = random.randint(3, 12) second_number = random.randint(3, 12) else: # hard first_number = random.randint(10, 20) second_number = random.randint(10, 20) # Rounds headings rounds_heading = f"\n๐Ÿ’ฟ๐Ÿ’ฟ๐Ÿ’ฟ Round {rounds_played + 1} of {num_rounds} ({difficulty} mode)๐Ÿ’ฟ๐Ÿ’ฟ๐Ÿ’ฟ" print(rounds_heading) # Ask multiplication question user_answer = int_checker(f"Question: {first_number} x {second_number} = ", 1, 400) print(f"You chose, {user_answer}") # Identify the correct answer correct_answer = first_number * second_number # Display if answer is correct / incorrect and add results to game history if user_answer == correct_answer: answers_correct += 1 feedback = "โœ…โœ…โœ… Congratulations, that is the correct answer โœ…โœ…โœ…" else: answers_incorrect += 1 feedback = (f"โŒโŒโŒ Sorry, that was an incorrect answer โŒโŒโŒ. " f"\033[1mThe correct answer is {correct_answer} \033[0m") print(feedback) # Set up history and feedback, and add it to the game history list (include the round number) history_item = f"Round: {rounds_played + 1} - {first_number} x {second_number} = {user_answer}, {feedback}" game_history.append(history_item) rounds_played += 1 # Game loop ends here # Game history / Statistics area # Calculate percentage of correct answers (accuracy) percentage_correct = answers_correct / rounds_played * 100 # Output game statistics print("\n๐Ÿ“Š๐Ÿ“Š๐Ÿ“Š Game Statistics ๐Ÿ“Š๐Ÿ“Š๐Ÿ“Š") print(f"๐ŸŽฎ Difficulty: {difficulty}") print(f"โœ… Correct answers: {answers_correct} \t " f"โŒ Incorrect answers: {answers_incorrect} \t " f"๐Ÿ‘ Accuracy: {percentage_correct:.2f}%") # Ask if user wants game history, if yes display history print() history = string_checker("Do you want to see your game history? ", yes_no) if history == "yes": for item in game_history: print(item) # Ask if users want to replay entire game print() restart_game = string_checker("Would you like to play again? ", yes_no) if restart_game == "yes": continue else: break # Display finishing line print("Thanks for playing!")