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): error = f'Please enter a number between {low} and {high}.' while True: try: response = int(input(question)) if response >= low and response <= high: return response else: print(error) except ValueError: print(error) # Variables difficulty_list = ["easy", "medium", "hard"] easy_difficulty = ["+", "-"] medium_difficulty = ["x", "+", "-"] game_history = [] rounds_played = 0 rounds_won = 0 rounds_lost = 0 num1 = random.randint(1, 10) num2 = random.randint(1, 10) print() print("Maths quiz") print() want_instructions = string_checker("Do you want to read the instructions? ") # checks users enter yes (y) or no (n) if want_instructions == "yes": print(''' *** Instructions **** To begin, choose the number of rounds (or press infinite mode). Then play against the computer. You need to pick R (rock), P (paper) or S (scissors). The rules are as follows: - Paper beats Rock - Rock beats scissors - Scissors beats paper Press to end the game at anytime. Good Luck! ''') rounds = int_check(f"How many rounds do you want? (3 to 10): ", 3, 10) 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}") print() while rounds_played < rounds: if rounds_difficulty == "easy": low_number = 1 high_number = 10 low_number_range = 0 high_number_range = high_number * high_number num1 = random.randint(low_number, high_number) num2 = random.randint(low_number, high_number) round_points = random.choice(easy_difficulty) if round_points == "-": if num1 > num2: answer = num1 - num2 else: answer = num2 - num1 else: if num1 > num2: answer = num1 + num2 else: answer = num2 + num1 elif rounds_difficulty == "medium": low_number = 4 high_number = 12 low_number_range = 0 high_number_range = high_number * high_number num1 = random.randint(low_number, high_number) num2 = random.randint(low_number, high_number) round_points = random.choice(medium_difficulty) if round_points == "x": if num1 > num2: answer = num1 * num2 else: answer = num2 * num1 elif round_points == "-": if num1 > num2: answer = num1 - num2 else: answer = num2 - num1 else: if num1 > num2: answer = num1 + num2 else: answer = num2 + num1 else: low_number = 6 high_number = 15 low_number_range = 0 high_number_range = high_number * high_number num1 = random.randint(low_number, high_number) num2 = random.randint(low_number, high_number) if num1 > num2: answer = num1 * num2 else: answer = num2 * num1 if num1 > num2: print(f"Round {rounds_played + 1} of {rounds} rounds") user_answer = int_check(f"What is {num1} {round_points} {num2}? ", low_number_range, high_number_range) else: print(f"Round {rounds_played + 1} of {rounds} rounds") user_answer = int_check(f"What is {num2} {round_points} {num1}? ", low_number_range, high_number_range) if answer == user_answer: rounds_won += 1 result_display = "Woohoo! correct answer" print(result_display) print() else: rounds_lost += 1 result_display = f"Incorrect, The correct answer was {answer} " print(result_display) print() if num1 > num2: history_item = f"Round {rounds_played + 1}: {num1} {round_points} {num2} = {user_answer} - {result_display}" else: history_item = f"Round {rounds_played + 1}: {num2} {round_points} {num1} = {user_answer} - {result_display}" game_history.append(history_item) 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 print("Game Statistices") 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? (yes / no): ") if see_history == "yes": for item in game_history: print(item) # end of program print() print("Thanks for playing!") print("The program has ended")