Math Quiz Game - Python Project

preview_player
Показать описание
#python #pythonprojects #pythonprogramming #pythonproject #pythonforbeginners #pythongame #pythonquizgame #mathquizgame
Math Quiz Game - Python Project by Maksim Samarenkin.
Рекомендации по теме
Комментарии
Автор

import turtle
import random

# Set up the Turtle screen
window = turtle.Screen()
window.title("Math Quiz Game")
window.bgcolor("lightblue")

# Create a Turtle object for drawing
pen = turtle.Turtle()
pen.speed(0)
pen.color("black")
pen.penup()
pen.hideturtle()

# Function to display a question
def display_question(question):
pen.goto(0, 100)
pen.clear()
pen.write(question, align="center", font=("Arial", 24, "normal"))

# Function to display a reward animation
def display_reward():
pen.goto(0, -50)
pen.clear()
pen.color("green")
pen.write("Correct!", align="center", font=("Arial", 24, "normal"))

# Function to generate a math question
def generate_question():
num1 = random.randint(1, 20)
num2 = random.randint(1, 20)
operator = random.choice(["+", "-"])

if operator == "+":
answer = num1 + num2
else:
answer = num1 - num2

question = f"What is {num1} {operator} {num2}?"

return question, answer

# Function to check the answer
def check_answer(user_answer, correct_answer):
return user_answer == correct_answer

# Main game loop
score = 0
for _ in range(5): # Ask 5 questions
question, answer = generate_question()
display_question(question)

user_answer = turtle.numinput("Math Quiz", "Enter your answer:")

if user_answer is not None:
user_answer = int(user_answer)
if check_answer(user_answer, answer):
score += 1
display_reward()

# Display the final score
pen.goto(0, 0)
pen.clear()
pen.color("blue")
pen.write(f"Your score: {score}/5", align="center", font=("Arial", 24, "normal"))

# Close the game when clicked
window.exitonclick()

BilgisayarHane