# -*- coding: utf-8 -*- """ Created on Fri Aug 9 12:28:59 2024 @author: henry """ """ This module contains a quiz game with multiple choice questions. The user can choose between different types of quizzes (addition or subtraction). """ import random def menu(): """Start the quiz with questions according to the user's choice.""" user_input = 0 while user_input not in ["1", "2", "3"]: # While the users input does not equal 1, 2, or 3 it will continue to ask the user which quiz they would like to play. print("Which quiz would you like to play?") print("1: Exit") print("2: Addition quiz") print("3: Subtraction quiz") user_input = input() if user_input in ["1", "2", "3"]: # Ensures that the users input MUST equal one of these options. if user_input == "1": # If the users input equals 1 they will exit the quiz. print("Exiting quiz game...") return elif user_input == "2": # If the users input equals 2 they will be taken to the addition quiz. print("Starting addition quiz...") addition_quiz() return else: # If the users input equals 3 they will be taken to the subtraction quiz. print("Starting subtraction quiz...") subtraction_quiz() return else: print("Invalid input") def addition_quiz(): """ Start the addition quiz. Provides the name, questions, and answers for the addition quiz. """ quiz_name = "Addition Quiz" passing_percentage = 50 questions = [ "What is 11+13: a:22 b:24 c:26 d:25", "What is 3+12: a:16 b:14 c:15 d:17", "What is 13+8: a:17 b:21 c:19 d:20", "What is 24+14: a:32 b:34 c:35 d:38", "What is 50+50: a:84 b:96 c:80 d:100" ] answers = ["b", "c", "b", "d", "d"] quiz_body(questions, answers, quiz_name, passing_percentage) return def subtraction_quiz(): """ Start the subtraction quiz. Provides the name, questions, and answers for the subtraction quiz. """ quiz_name = "Subtraction Quiz" passing_percentage = 50 questions = [ "What is 1-0: a:1 b:0 c:-1 d:2", "What is 2-7: a:-6 b:5 c:-5 d:-4", "What is 3-1: a:2 b:4 c:-3 d:5", "What is 4-5: a:4 b:-1 c:3 d:-2", "What is 5-3: a:-4 b:4 c:7 d:2" ] answers = ["a", "c", "a", "b", "d"] quiz_body(questions, answers, quiz_name, passing_percentage) return def quiz_body(questions, answers, quiz_name, passing_percentage): """ Conduct the quiz. Parameters ---------- questions : list An array containing all the questions for the quiz. answers : list An array containing the correct answers for each question, in order. quiz_name : str The name of the quiz, displayed at the start. Returns ------- None """ if(len(questions) == len(answers) and len(questions) > 0 and len(answers) > 0): # Makes sure that the answers and questions arrays are equal and above zero. initial_question_len = len(questions) questions_correct = 0 print("Welcome to the " + str(quiz_name)) while len(questions) > 0: # If the len of questions is below 1 it will exit/skip the while loop. current_question = random.randint(0, len(questions) - 1) print(str(questions[current_question])) user_input = input().lower() if user_input in ["a", "b", "c", "d"]: # If the user_input is a, b, c or d then it is a valid answer. if user_input == answers[current_question]: # If the users answer equals the answer in the answers array they will get the answer correct. print("Correct") questions_correct = questions_correct + 1 else: # If the users answer is incorrect they will get the answer wrong. print("Incorrect") questions.pop(current_question) answers.pop(current_question) else: # Invalid answer and will loop back around printing everything in the while loop and picking a new question. print("Invalid input") questions_correct_p = (questions_correct / initial_question_len) * 100 print("You answered " + str(questions_correct) + "/" + str(initial_question_len) + " questions correctly!") print("You got " + str(questions_correct_p) + "% of the questions correct!") if questions_correct_p >= passing_percentage: # The user will "pass" if questions_correct_p is above passing_percentage. print("You passed!") else: # The user will fail if the questions_correct_p is below passing_percentage. print("You failed!") else: # Will tell the user somthing is wrong if the questions and answers arrays are not equal. print("questions and answers arrays are not equal or are below 1") print("returing to menu...") menu() return