In this game, the computer asks you 10 math questions. You have to answer as many right as you can, after you answer all of them, you can see the grade you got out of 100%.
In order to play the game you must copy the code below and paste it into your python file. After that, you run the code and enjoy!
Copy the code for FREE!:
import turtle
import random
def generate_problem():
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
operator = random.choice([‘+’, ‘-‘, ‘‘, ‘/’]) if operator == ‘+’: answer = num1 + num2 elif operator == ‘-‘: answer = num1 – num2 elif operator == ‘‘:
answer = num1 * num2
else:
num1 = num2 * random.randint(1, 10)
answer = int(num1 / num2)
problem = f”What is {num1} {operator} {num2}?”
return problem, answer
def display_grade(correct_answers):
grade = (correct_answers / 10) * 100
turtle.clear()
turtle.write(f”Grade: {grade}%”, align=’center’, font=(‘Arial’, 24, ‘bold’))
turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.write(“Click anywhere to play again.”, align=’center’, font=(‘Arial’, 16, ‘normal’))
def click_handler(x, y):
turtle.clear()
play_game()
def play_game():
turtle.clear()
correct_answers = 0
for _ in range(10):
problem, answer = generate_problem()
user_answer = turtle.textinput(“Math Problem”, problem)
if user_answer is not None:
user_answer = int(user_answer)
if user_answer == answer:
correct_answers += 1
display_grade(correct_answers)
turtle.onscreenclick(click_handler)
screen = turtle.Screen()
screen.title(“Math Quiz”)
screen.bgcolor(“white”)
turtle.penup()
turtle.hideturtle()
play_game()
turtle.mainloop()