import random import math # Check that users have entered a valid option based on a 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 the user does not enter something that is valid print(error) print() # Displays instructions def instructions(): """Prints instructions""" print(""" *** šŸ“œ Instructions šŸ“œ *** To begin, choose the number of questions that you want. You can not choose less than 5 or more than 20 questions. Then, you will be asked what the square root of a number equals. Enter your answer and ensure that it's rounded to two decimal places. (please be very careful with your rounding) At the end of the quiz, you will be provided with your score. Try and improve upon your previous score when you play. Press to end the game at anytime. Good luck! """) # checks for an integer more than {low} and less than {high} # ensures that if the number is outside the range, an error message will be printed def int_check(question, low, high): while True: error = f"Please enter a whole number that is more than {low - 1} and less than {high + 1}" to_check = input(question) try: response = int(to_check) # checks that number is more than / equal to 1 if response < low: print(error) elif response > high: print (error) else: return response except ValueError: print(error) # checks if a float is less than one # checks for exits code def float_check(question): while True: error = f"Please enter a number" to_check = input(question) # checks for exit code if to_check == "xxx": return "xxx" try: response = float(to_check) if response < 1: print (error) else: return response except ValueError: print(error) # Main Routine Starts here # Initialises game variables, including [yes, no] list mode = "regular" rounds_played = 0 rounds_incorrect = 0 yes_no = ['yes', 'no'] game_history = [] # prints the title of the game print("√ √ √ Square Roots Math Quiz √ √ √ ") # ask the user if they want to see the instructions and display them if requested want_instructions = string_checker("Do you want to read the instructions? ", yes_no) # checks users enter yes (y) or no (n) if want_instructions == "yes": instructions() # Ask user for number of rounds # sets an error if answer is our of boundaries error = "Please enter a number lower than 21, but higher than 4." num_rounds = int_check("How many rounds would you like? ", low=5, high=20) # 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) # get question and answer # sets variables for sqrt_quiz num = random.randint(1, 100) result = math.sqrt(num) rounded_result = round(result, 2) sqrt_quiz = float_check(f"What is the square root of {num}? ") # check if user choice is correct or rounded and correct if sqrt_quiz == result: feedback = f"āœ… Correct! The square root of {num} is {result}! āœ…" elif sqrt_quiz == rounded_result: feedback = f"āœ… Correct! The square root of {num} is {rounded_result}! āœ…" # check if user choice is the exit code, break the loop elif sqrt_quiz == "xxx": break # checks if user choice is incorrect elif sqrt_quiz != result or rounded_result: rounds_incorrect += 1 feedback = f"āŒ Incorrect. The square root of {num} is {result} or {rounded_result} āŒ" # set up rounds feedback and output it to the user # Add it to the game history list (including the round number) round_feedback = f"What is the square root of {num}?, {feedback}" history_item = f"Round: {rounds_played + 1} - {round_feedback}" print(feedback) game_history.append(history_item) rounds_played += 1 # Game loop ends here print() # Game History / Statistics area if rounds_played > 0: # calculate statistics rounds_correct = rounds_played - rounds_incorrect percent_correct = rounds_correct / rounds_played * 100 percent_incorrect = rounds_incorrect / rounds_played * 100 # Output Game Statistics print(" +++ Game Statistics +++ ") print(f"Correct: {percent_correct:.2f} \t " f"Incorrect: {percent_incorrect:.2f} \t ") # ask user if they want to see their game history, and output it 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) print() print("Thanks for playing.") # prints a goodbye message if you use the exit code before answering the quiz questions else: print("Oops - You chickened out!")