import random # Check that users have entered a valid # option based on a list def string_checker(question, valid_ans): while True: error = f"Enter a valid option from {valid_ans}" user_response = input(question).lower() for item in valid_ans: # check if the user response is a valid answer if item == user_response: return item elif user_response == item[0]: return item print(error) print() def instructions(): print(''' **** Instructions **** welcome to Multiplication Game Here is how to play: You choose how many rounds you would like to play and each round you will get one question You type the answer that you think is right and press enter The game will tell you whether you have gotten it right or not. One of our speciality of this game is that it can show you the history and you can revise on the ones you have gotten wrong. Have fun learning! ''' ) # checks the input is an integer def int_check(question): while True: error = "Enter an integer more than 1" try: response = int(input(question)) # check the number is more than 1 if response >= 1: return response else: print(error) except ValueError: print(error) # Main routine # game variables rounds_played = 0 wrong_answers = 0 correct_answers = 0 yes_no = ["yes", "no"] game_history = [] print() print("** The multiplication Game **") print() print("** Welcome to the multiplication game **") print() # instructions want_instructions = string_checker("Do you want to see the instructions? ", yes_no) if want_instructions == "yes": instructions() num_rounds = int_check("Enter the number of rounds. You can choose between 3 to 5 rounds ") # game loop starts here while rounds_played < num_rounds: rounds_heading = f"\n*** Round {rounds_played + 1} of {num_rounds} ***" print(rounds_heading) # variables for the creating the question and checking for the right answer random_1 = random.randint(1, 12) random_2 = random.randint(5, 15) correct_answer = random_1 * random_2 user_answer = int(input(f"{random_1} x {random_2} = ")) if user_answer == correct_answer: print("correct") correct_answers += 1 rounds_played += 1 else: print(f"Incorrect!! The answer was {correct_answer}") wrong_answers += 1 rounds_played += 1 if rounds_played > 0: # Calculate stats correct_answers = rounds_played - wrong_answers percentage_won = correct_answers / rounds_played * 100 percentage_lost = wrong_answers / rounds_played * 100 # Display Game Stats print() print("**** GAME STATS ****") print(f"You got {correct_answers} correct out of {rounds_played} rounds") print(f"Won: {percentage_won:.2f} % \t" f"Lost: {percentage_lost:.2f} % \t") print() # Game history option see_history = string_checker("Do you want to see your game history? ", yes_no) if see_history == "yes": for item in game_history: print(item) else: print("😾😾😾 Scaredy Cat! 😾😾😾") # End of program message print() print("The program has ended")