import random # ------------------------ Functions ------------------------ # # Adds decoration to headings def statement_generator(statement, decoration, amount): print(f"\n{decoration * amount} {statement} {decoration * amount}") # Checks users enter an integer more than or equal to a specified limit def int_check(question, low=None, high=None, exit_code=None): if low is None and high is None: error = "Please enter an integer\n" elif low is not None and high is None: error = f"Please enter an integer that is more than or equal to {low}\n" else: error = f"Please enter an integer between {low} and {high} (inclusive)" while True: response = input(question).lower() if response == exit_code: return response try: response = int(response) if low is not None and response < low: print(error) elif high is not None and response > high: print(error) else: return response except ValueError: print(error) # Checks user input against a list of valid responses def string_checker(question, valid_ans=("yes", "no")): error = f"Please enter one of the following: {valid_ans}" while True: user_response = input(question).lower() for ans in valid_ans: if ans == user_response: return ans elif user_response == ans[0]: return ans print(error) print() # Displays game instructions def instructions(): statement_generator("Instructions", "📋", 3) print(""" To begin, choose the number of questions (or press for infinite mode). Then, choose the difficulty (1 = Easy, 2 = Medium, 3 = Hard). You will be given random math questions. try to solve them and enter a answer. key = (+)=plus (-)=minus (*)=times (/)=divide You can exit at any time by typing 'xxx' but you'll be a chicken XD. Good luck :D """) # ------------------------ Main Routine ------------------------ # # Initialise game variables gamemode = "regular" rounds_played = 0 questions_right = 0 questions_wrong = 0 operations = ["+", "-", "*", "/"] quiz_history = [] statistics = [] # Game heading statement_generator("Basic Math Quiz", "🔢", 3) # Ask if user wants to view instructions want_instructions = string_checker("Would you like to view the instructions? ") if want_instructions == "yes": instructions() # Ask user for number of rounds rounds = int_check("\nHow many questions would you like to attempt? (Press for infinite) ", low=1, exit_code="") if rounds == "": gamemode = "infinite" rounds = 1 # Ask user for difficulty level difficulty = int_check("\nDifficulty? (1=Easy, 2=Medium, 3=Hard): ", low=1, high=3) # Quiz loop starts here while rounds_played < rounds: # Round heading statement_generator(f"Question {rounds_played + 1}", "=", 3) # Generate a random operation random_operation = random.choice(operations) # Determine number ranges by difficulty if difficulty == 1: low, high = 1, 10 elif difficulty == 2: low, high = 10, 20 else: low, high = 50, 100 # Create operands num1 = random.randint(low, high) num2 = random.randint(low, high) # Adjust for division to avoid decimals if random_operation == "/": num1 = num1 * num2 # ensures num1 is divisible by num2 # Build and solve the equation expression = f"{num1} {random_operation} {num2}" correct_answer = int(eval(expression)) # Ask the user user_answer = int_check(f"{expression} = ", exit_code="xxx") if user_answer == "xxx": break # Check user's answer if int(user_answer) == correct_answer: statement_generator("Correct!", "✅", 2) questions_right += 1 statistics.append(1) else: statement_generator("Incorrect!", "❌", 2) questions_wrong += 1 statistics.append(0) # Record history quiz_history.append(f"Q{rounds_played + 1}: {expression} = {correct_answer} | Your answer: {user_answer}") rounds_played += 1 if gamemode == "infinite": rounds += 1 # ------------------------ Results Summary ------------------------ # if rounds_played > 0: percentage = (sum(statistics) / len(statistics)) * 100 percentage = round(percentage) print() statement_generator("📊 Game Statistics 📊", "=", 3) print(f"Correct: {questions_right}\tIncorrect: {questions_wrong}\tScore: {percentage}%") see_history = string_checker("\nWould you like to view your game history? ") if see_history == "yes": print() for item in quiz_history: print(item) else: print("\n🐔🐔🐔 You didn’t answer any questions! chicken 🐔🐔🐔")