import random
from tkinter import *
root = Tk()
root.title("Final Project")
canvas1 = Canvas(root, width=400, height=200)
canvas1.grid(row=0, column = 0)
#inserting text
e = Entry(root, width=25)
e.insert(0, "Student ID Entry")
r = Entry(root, width=25,)
g = Entry(root, width=25)
#Button Functions (I am having issues with these ones)
def addition():
x1 = random.randrange(1, 101, 1)
x2 = random.randrange(1, 101, 1)
r.insert(0, x1)
r.insert(0, "+")
r.insert(0, x2)
def subtraction():
x1 = random.randrange(1, 101, 1)
x2 = random.randrange(1, 101, 1)
r.insert(0, x1)
r.insert(0, "-")
r.insert(0, x2)
#the equation doesn't show up for this one when pressed. Yet it lags and doesn't show.
def multiplication():
x1 = random.randrange(1, 101, 1)
x2 = random.randrange(1, 101, 1)
r.insert(0, x1)
r.insert(0, "*")
r.insert(0, x2)
y = int(input("Enter a num"))
if x1 * x2 == y:
print("congrats")
else:
print("Ur wrong")
#shows the division operator
def division():
x1 = random.randrange(1, 101, 1)
x2 = random.randrange(1, 101, 1)
r.insert(0, x1)
r.insert(0, "/")
r.insert(0, x2)
y = int(input("Enter a num"))
if x1 / x2 == y:
print("congrats")
else:
print("Ur wrong")
#this is the button to check if the solution is correct, and I'm having issues with this one
def calculate():
global ans
ans = int(g.get())
x1 = random.randrange(1, 101, 1)
x2 = random.randrange(1, 101, 1)
solution= x1+x2
if r.get() == solution:
label_Congrats = Label(root, text= "Congratulations", bg='yellow')
canvas1.create_window(270, 200, window=label_Congrats)
else:
label_Congrats = Label(root, text= "Sorry", bg='yellow')
canvas1.create_window(270, 200, window=label_Congrats)
ans= StringVar()
#labels
studentID = Label(root, text="Enter Student ID", width=25, bg="blue", fg="white")
generating = Label(root, text="Generated numbers", width=30, bg="orange", fg="white")
guess = Label(root, text="Student Guess", width=15, bg="black", fg="white")
#Characteristics of a button
addition = Button(text="Addition",command=addition,width=20,height=3,bg="blue",fg="white")
subtraction = Button(text="Subtraction",command=subtraction,width=20,height=3,bg="blue", fg="white")
multiplication = Button(text="Multiplication",command=multiplication,width=20,height=3,bg="blue",
fg="white")
division = Button(
text="Division",
command=division,
width=20,
height=3,
bg="blue",
fg="white")
calculate = Button(
text="Calculate",
command=calculate,
width=15,
height=2,
bg="blue",
fg="white")
#Label Location
studentID.grid(row=1, column=2)
e.grid(row=1, column=3)
r.grid(row=4, column=3)
g.grid(row=5, column=3)
generating.grid(row=4, column=2)
guess.grid(row=5, column=2)
#Buttons Location
addition.grid(row=2, column=1)
subtraction.grid(row=2, column=2)
multiplication.grid(row=2, column=3)
division.grid(row=2, column=4)
calculate.grid(row=6, column=2)
root.mainloop()
Предполагается, что код запускает математическую игру, и когда каждый оператор нажимается, он показывает уравнение, коррелирующее с оператором. Вы вводите решение и нажимаете «Рассчитать», чтобы увидеть, правильно вы поняли или нет. Вы также вводите свое имя в студенческий билет, и он сохраняет ваши данные. У меня в основном проблемы с операторами.