could you check if the following code is ai generated def main(): while True: choice =…
- User
could you check if the following code is ai generated def main(): while True: choice = display_menu() if choice == '1': start_quiz() elif choice == '2': show_instructions() elif choice == '3': print("Thank you for playing! Goodbye!") break else: print("Invalid choice. Please select again.")
def display_menu(): """Displays the main menu and returns the user's choice.""" print("\nChoose:") print("1. Play") print("2. Game instructions") print("3. Exit") return input("Enter your choice: ") import random
def start_quiz(): """Starts the Math Quiz game.""" difficulty = choose_difficulty() questions, correct_answers, user_answers = generate_questions(difficulty) correct_count = evaluate_answers(correct_answers, user_answers) show_results(correct_count, len(questions))
def choose_difficulty(): """Allows the player to select a difficulty level.""" print("\nSelect Difficulty Level:") print("1. Easy") print("2. Medium") print("3. Hard") choice = input("Enter your choice: ") if choice == '1': return "Easy" elif choice == '2': return "Medium" elif choice == '3': return "Hard" else: print("Invalid choice, defaulting to Easy.") return "Easy"
def generate_questions(difficulty): """Generates questions based on the selected difficulty.""" num_questions = 5 questions = [] correct_answers = [] user_answers = []
codefor _ in range(num_questions): num1 = random.randint(1, 10) num2 = random.randint(1, 10) if difficulty == "Easy": operation = '+' elif difficulty == "Medium": operation = random.choice(['+', '-']) else: operation = random.choice(['+', '-', '*']) question = f"{num1} {operation} {num2}" answer = eval(question) # Evaluate the string as a math expression questions.append(question) correct_answers.append(answer) try: user_input = int(input(f"Solve: {question} = ")) user_answers.append(user_input) except ValueError: print("Invalid input, counted as incorrect.") user_answers.append(None) return questions, correct_answers, user_answersdef evaluate_answers(correct_answers, user_answers): """Evaluates the user's answers and returns the number of correct responses.""" correct_count = 0 for correct, user in zip(correct_answers, user_answers): if correct == user: correct_count += 1 return correct_count
- Assistant
The code you provided is a simple implementation of a math quiz game in Python. It's structured clearly and adheres to basic programming principles, such as defining functions for specific tasks and implementing user interaction through a console menu.