import random 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() def int_check(question): while True: to_check = input(question) # Check for infinite mode if to_check == "": return "infinite" try: response = int(to_check) # Check that the number is more than or equal to 1 if response < 1: print("Please enter an integer that is 1 or more.") else: return response except ValueError: print("Please enter a valid integer or press Enter for infinite mode.") # def instruction is the variable for the instructions def instructions(): instruction = """Perimeter and Area Quiz Instructions Welcome to the Perimeter and Area Quiz! This program is designed to help you understand and practice calculating the perimeter and area of different shapes. It is suitable for intermediate students or anyone looking to improve their skills in this area. How it Works: 1. Introduction: The quiz is used to practice your practical skills around rectangles, triangles and squares. Here's the formulas for given shapes. Triangle Area: ½ x base x height Perimeter: Side A + Side B = Side C Square Area: Side² Perimeter: 4 x side Rectangle Area: length x width Perimeter: 2 x (length + width) 2. Mode Selection: You will have the option to choose between: - Infinite Mode: Answer an unlimited number of questions. - Set Round: Answer a fixed number of questions. 3. Quiz: You will be presented with questions where you need to calculate the perimeter or area of a given shape. Enter your answers based on the provided shapes. 4. Exit and History: After completing the quiz, you can choose to exit the program. You will then have the option to view your game history to see your performance. Good luck, and have fun learning! """ print(instruction) # shape and question generation for the question of area and perimeter def generate_shape_question(): # defining the shapes shapes = ['triangle', 'rectangle', 'square'] # the units the question will have units = ['km', 'cm', 'm'] # program randomly chooses shapes shape = random.choice(shapes) # program randomly chooses units for the question unit = random.choice(units) question = "" correct_answer = () # This part of the program randomise the units for question, then the program will calculate the area and perimeter, # from there placing the correct answers in a variable if shape == 'triangle': base = random.randint(1, 20) height = random.randint(1, 20) side_a = random.randint(1, 20) side_b = random.randint(1, 20) side_c = random.randint(1, 20) area = 0.5 * base * height perimeter = side_a + side_b + side_c question = (f"A triangle has a base of {base} {unit} and a height of {height} {unit}. " f"Its sides are {side_a}, {side_b}, and {side_c} {unit}. " f"Calculate the area and perimeter of the triangle.TEST {area} AREA and perimeter {perimeter}") correct_answer = (area, perimeter) # This part of the program randomise the units for question, then the program will calculate the area and perimeter, # from there placing the correct answers in a variable elif shape == 'rectangle': length = random.randint(1, 20) width = random.randint(1, 20) area = length * width perimeter = 2 * (length + width) question = (f"A rectangle has a length of {length} {unit} and a width of {width} {unit}. " f"Calculate the area and perimeter of the rectangle.TEST {area} AREA and perimeter {perimeter}") correct_answer = (area, perimeter) # This part of the program +randomise the units for question, then the program will calculate the area and perimeter, # from there placing the correct answers in a variable else: # shape == 'square' side = random.randint(1, 20) area = side ** 2 perimeter = 4 * side question = (f"A square has a side length of {side} {unit}. " f"Calculate the area and perimeter of the square.TEST {area} AREA and perimeter {perimeter}") correct_answer = (area, perimeter) return question, correct_answer def main(): # Initialise game variables mode = "regular" rounds_played = 0 game_history = [] # prints the heading for the game print("📚 Area and Perimeter quiz 📚") print() # these variable prints the question want_instructions = string_checker("Do you want to read the instructions? (yes/no):") # checkers users enter yes (Y) or no (N) if want_instructions == "yes": instructions() # Ask user for number of rounds / infinite mode num_rounds = int_check("How many rounds would you like? Push for infinite mode: ") # if variable is num_rounds infinite then the program will float the variable to make it inf if num_rounds == "infinite": mode = "infinite" num_rounds = float('inf') # program loop starts here while rounds_played < num_rounds: rounds_played += 1 # Generate a new question question, correct_answer = generate_shape_question() # Rounds heading if mode == "infinite": rounds_heading = f"\n✨✨✨ Round {rounds_played} (Infinite Mode) ✨✨✨" else: rounds_heading = f"\n🔶🔶🔶 Round {rounds_played} of {num_rounds} 🔶🔶🔶" print(rounds_heading) print(question) # Placeholder for user's answer and validation try: user_area = float(input("Enter the area: ")) user_perimeter = float(input("Enter the perimeter: ")) except ValueError: print("please enter valid numbers for area and perimeter.") continue # compares the computers answer for the question to the users answer, if the answers are the same the program # will follow through by printing the correct answer variable area_correct = user_area == correct_answer[0] perimeter_correct = user_perimeter == correct_answer[1] # this part of the program will print the computer answers or if the user got them correct then print correct. if area_correct and perimeter_correct: result = "Correct!" elif area_correct and not perimeter_correct: result = "Area is correct but the perimeter is incorrect. The correct perimeter is {correct_answer[1]}." elif not area_correct and perimeter_correct: result = f"Perimeter is correct but the area is incorrect. The correct area is {correct_answer[0]}." else: result = f"Both area and perimeter are incorrect. The correct area is {correct_answer[0]} and the correct" f"perimeter is {correct_answer[1]}." # game history # Option to exit the infinite mode if mode == "infinite": continue_quiz = string_checker("Do you want to continue? (yes/no): ") if continue_quiz == "no": break if __name__ == "__main__": main()