import random # Functions def statement_generator(statement, decoration, amount): """Adds additional characters to the start and end of headings as decoration""" print(f"\n{decoration * amount} {statement} {decoration * amount}") def int_check(question, low=None, high=None, exit_code=None): """Checks users enter an integer within optional bounds""" if low is None and high is None: error = "Please enter an integer" elif low is not None and high is None: error = f"Please enter an integer that is more than / equal to {low}" else: error = f"Please enter an integer that is 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) def string_checker(question, valid_ans=('yes', 'no')): """Check that users have entered a valid option based on a list""" 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 or user_response == ans[0]: return ans print(error) print() def instructions(): """Prints instructions""" statement_generator("Instructions", "*", 3) print(""" insructions... """) # Game Setup game_mode = "regular" rounds_played = 0 questions_right = 0 questions_wrong = 0 quiz_history = [] statistics = [] operations = ["+", "-", "*", "/"] # Main Routine statement_generator("Basic Math Quiz", "❓", 3) want_instructions = string_checker("Would you like to view the instructions? ") if want_instructions == "yes": instructions() rounds = int_check("\nHow many questions would you like? (Press for infinite) ", low=1, exit_code="") if rounds == "": game_mode = "infinite" rounds = 1 # Quiz Loop while rounds_played < rounds: statement_generator(f"Question {rounds_played + 1}", "*", 3) operation = random.choice(operations) if operation != "/": num1 = random.randint(1, 20) num2 = random.randint(1, 20) else: num2 = random.randint(1, 10) result = random.randint(1, 10) num1 = result * num2 # ensures clean division expression = f"{num1} {operation} {num2}" answer = int(eval(expression)) user_guess = int_check(f"{expression} = ", low=None, high=None, exit_code="xxx") if user_guess == "xxx": break if int(user_guess) == answer: statement_generator("Correct!", "✅", 2) questions_right += 1 statistics.append(1) else: statement_generator(f"Incorrect! The correct answer was {answer}.", "❌", 2) questions_wrong += 1 statistics.append(0) quiz_history.append(f"Q{rounds_played + 1}: {expression} = {answer} | Your Answer: {user_guess}") rounds_played += 1 if game_mode == "infinite": rounds += 1 # Game Over or Finished if rounds_played > 0: percentage = round((sum(statistics) / len(statistics)) * 100) statement_generator("Quiz Complete!", "🎉", 3) statement_generator("Statistics", "📊", 3) print(f"Correct: {questions_right} | Incorrect: {questions_wrong} | Accuracy: {percentage}%\n") see_history = string_checker("Would you like to see your quiz history? ") if see_history == "yes": for item in quiz_history: print(item) else: print("You quit before answering any questions. 🐔")