"""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, display_boundary): while True: if display_boundary == 1: error = f"Please enter an integer that is between {low} and {high}." else: error = "Please enter an integer." to_check = input(question) 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) """This function detects if the answer given is either Yes, No, or the first letter of each.""" def string_checker(question, valid_ans): 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 """This function generates the results of the user's performance in the quiz""" def results(): """prints instructions""" print(f""" *** Results *** You ended with a score of {points}/{num_questions}. (which is {round(points / num_questions * 100, 2)}%) """) """This function holds the instructions.""" def instructions(): """prints instructions""" print(""" *** Instructions *** To begin, choose the number of rounds between 5 and 15. Then answer the questions generated. If you answer correctly, you advance to the next question. If you answer incorrectly, you still advance to the next question, but you get a really bright red cross instead. You will get your final score after your rounds have ended. """) mode = "regular" question_number = 0 points = 0 want_results = 0 yes_no = ["yes", "no"] # Main Routine goes here print() print("Multiplication Quiz") print() # Asks if user wants to see the instructions want_instructions = string_checker("Do you want to see the instructions? ", yes_no) print() # Display the instructions if the user responded with yes if want_instructions == 'yes': instructions() input("Type anything to continue: ") print() # Ask user for number of questions and starts quiz num_questions = int_check("Enter a number between 5 and 15. " "This will be the number of questions" " you will answer: ", 5, 15, 1) print() print("QUIZ STARTED") # 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, 12) integer_2 = random.randint(1, 12) correct_answer = integer_1 * integer_2 # asks question # determines if the answer given is equal to the correct answer user_choice = int_check(f"{integer_1} x {integer_2} = ", 1, 100, 0) if int(user_choice) == int(correct_answer): print("✅ Correct!") points = points + 1 else: print(f"❌ The correct answer was {correct_answer}") 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(yes/no)? ", yes_no) if want_results == 'yes': results() # end