"""This programme runs a multiplication quiz.""" import random # define functions and variables """This function detects if the answer given is an integer and between a range.""" def int_check(question, low, high): while True: error = f"Please enter an integer that is between {low} and {high}." to_check = input(question) if to_check == "": return error try: response = int(to_check) # checks if input is above or below desired range if low <= response <= high: return response else: print(error) except ValueError: print(error) def string_checker(question, valid_ans=("yes", "no")): while True: user_response = input(question).lower() for item in valid_ans: if item == user_response: return item elif user_response == item[0]: return item def results(): """prints instructions""" print(f""" *** Results *** You ended with a score of {points}/{num_questions}. (which is {round(points / num_questions * 100, 2)}%) """) mode = "regular" question_number = 0 points = 0 want_results = 0 # Main Routine goes here print("Multiplication Quiz") print() # Instructions # Ask user for number of questions / infinite mode num_questions = int_check("Enter a number between 5 and 15. " "This will be the number of questions" " you will answer: ", 5, 15) # game loop starts here while question_number < num_questions: question_heading = f"\n Question {question_number + 1} of {num_questions}" print(question_heading) print() # set temporary variables integer_1 = random.randint(1, 10) integer_2 = random.randint(1, 10) correct_answer = integer_1 * integer_2 # asks question # determines if the answer given is equal to the correct answer user_choice = int(input(f"{integer_1} x {integer_2} = ")) if int(user_choice) == int(correct_answer): print("✅ Correct!") points = points + 1 else: print(f"❌ The correct answer was {correct_answer}") if user_choice == "xxx": break question_number += 1 # game loop ends here # game history / statistic area print() print("You have finished your quiz!") want_results = string_checker("Would you like to see your results? ") if want_results == 'yes': results()