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 f user does not enter a valid response print(error) print() # Checks that the user enters an integer more than 0 (allows ) def int_check(question, low, high): while True: error = "please input and integer (whole number) between or equal to 1 or 15." to_check = input(question) try: response = int(to_check) # checks that their number is more than / equal to 1 if high >= response <= low: print(error) else: return response except ValueError: print(error) # Compare user/computer choice and returns # Result (correct / incorrect) def ans_compare(user, answer): # If the user and computer choice is the same it is a tie if user == answer: round_result = 'Correct' # If it is not a win / tie, the computer wins else: round_result = "Incorrect" return round_result # Initialise game variables mode = "regular" rounds_played = 0 rounds_tried = 0 rounds_lost = 0 num_list_1 = [1, 2, 3, 4,5, 6, 7, 8, 9, 10, "xxx"] num_list_2 = [1, 2, 3, 4,5, 6, 7, 8, 9, 10, "xxx"] game_history = [] print("Multiplication Quiz") print() def instructions(): print() print("Rock/Paper/Scissors game") # 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": """Prints instructions""" print(""" ****Instructions**** To begin, choose the number of rounds you would like to play, and one of following modes: E(easy), M(moderate), and H(hard). Then play against the computer. You need to the rules are as follows: o Paper beats rock o rock beats scissors o scissors beats paper Enter if you would like to exit Good luck! """) # Ask for the number of round they 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 one number form both number lists, and add / multiply / # square them (depends on difficulty) to create # a solution to a random question. num1 = random.choice(num_list_1[:-1]) num2 = random.choice(num_list_2[:-1]) ans = num1 + num2 # gets user choice user_input = string_checker(f"What does {num1} + {num2} equal?") print("you chose", user_input) # if user choice is the exit code, break the loop if user_input == "xxx": break result = ans_compare(user_input, ans) # Adjust game lost / game tried and adds results to the game history if result == "Correct": rounds_tried += 1 feedback = "Correct" else: rounds_lost += 1 feedback = "Incorrect" # set up round feedback and output it user # add it to the game history list (include the round number) round_feedback = f"{user_input} = {ans}?, {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 if rounds_played > 0: # calculate statistics rounds_won = rounds_played - rounds_tried - rounds_lost percent_won = rounds_won / rounds_played * 100 percent_lost = rounds_lost / rounds_played * 100 percent_tied = rounds_tried / rounds_played * 100 # output game statistics print("Game Statistics") print(f"Won: {percent_won:.2f} \t" f"Lost: {percent_lost:.2f} \t" f"Tied: {percent_tied:.2f}") # 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") else: print("Oops, you chickened out!")