所以我去找了一个熟悉编程的人帮助,但找不到解决方案,我已经搜索了如何尝试和做它,但我无法理解他们的意思,我正在制作一个小程序,它教简单的加法和减法,我对python很陌生。那么我如何得到这个"用户输入等于答案,然后显示正确或不正确"的工作,因为它只是循环和循环的问题,因为我只想要5个问题。不要评判,我知道这是混乱的,但代码是下面。请帮助。
from tkinter import*
import random
def quit():
window.destroy()
def restart():
window.destroy()
main()
#generates random numbers and operator
def randomnumbergen():
global rand1, rand2, answer, userint, ops, NEWOP
ops = ['+', '-']
rand1 = random.randint(1,10)
rand2 = random.randint(1,rand1)
operation = random.choice(ops)
answer = eval(str(rand1) + operation + str(rand2))
if operation == "+":
NEWOP = "+"
elif operation == "-":
NEWOP = "-"
#layout of the screen and what it looks like
def display():
global userint
canvas.delete('all')
randomnumbergen()
#first question
canvas.create_rectangle(175,85,475,135, fill='#b80505')
canvas.create_text(210,115, text=rand1, fill="white", font='chiller 40 bold')
canvas.create_text(255,115, text=NEWOP, fill="white", font='chiller 40 bold')
canvas.create_text(320,115, text=rand2, fill="white", font='chiller 40 bold')
canvas.create_text(350,110, text="=", fill="white", font='chiller 40 bold')
userint = Entry(canvas, font="chiller 40 bold", bg="#b80505")
canvas.create_window(420,110, window=userint, height=45, width=105)
#buttons to exit, go back and submit
canvas.create_window(325,350,width=100,height=50,window=(Button(window, text="Submit", font='chiller 30 bold', width=20, bg='#b80505', command=displayupdate)) )
canvas.create_window(550,350,width=100,height=50,window=(Button(window, text="Exit", font='chiller 30 bold', width=20, bg='#b80505', command=quit)) )
canvas.create_window(100,350,width=100,height=50,window=(Button(window, text="Go Back", font='chiller 30 bold', width=20, bg='#b80505', command=restart)) )
canvas.update("")
count = count + 5
#if the user answers the question it tells them correct or incorrect
def displayupdate():
global userint
#Keeping Score
if answer == userint:
canvas.create_text(570,110, text="Correct!", font='chiller 30 bold', fill='green')
else:
canvas.create_text(570,110, text="Incorrect!!", font='chiller 30 bold', fill='red')
if count <= 5:
display()
# easy questions
def Easy():
display()
canvas.create_text(325, 50, text="Easy", fill='black', font='chiller 50 bold')
#medium level questions
def Medium():
display()
canvas.create_text(325, 50, text="Medium", fill='black', font='chiller 50 bold')
#harder level questions
def Hard():
display()
canvas.create_text(325, 50, text="Hard", fill='black', font='chiller 50 bold')
#page with helpful tips in working out the answers
def Help(event):
if event.x >610 and event.x <650 and event.y >5 and event.y <40 :
canvas.delete('all')
canvas.create_window(310,350,width=100,height=50,window=(Button(window, text="Exit", font='chiller 30 bold', width=20, bg='#b80505', command=quit)) )
canvas.update()
else:
if event.x != 610 and event.x != 650 and event.y != 5 and event.y != 40:
""
# main page where user chooses from Easy, Medium or Hard
#or if they want to quit
def main ():
global window
global tkinter
global canvas
global rand1, rand2, count
count = 0
window = Tk()
canvas = Canvas(window, width=650, height=400, bg='#b80505')
canvas.create_text(325, 75, text="Mad Maths", fill='black', font='chiller 50 bold')
easy = canvas.create_window(200,200,width=100,height=50,window=(Button(window, text="Easy", font='chiller 30 bold', width=20, bg='#298704', command=Easy)) )
medium = canvas.create_window(310,200,width=100,height=50,window=(Button(window, text="Medium", font='chiller 30 bold', width=20, bg='orange', command=Medium)) )
hard = canvas.create_window(420,200,width=100,height=50,window=(Button(window, text="Hard", font='chiller 30 bold', width=20, bg='red', command=Hard)) )
canvas.create_window(310,300,width=100,height=50,window=(Button(window, text="Exit", font='chiller 30 bold', width=20, bg='#b80505', command=quit)) )
canvas.create_oval(610,5,650,40, fill='yellow', outline='red', width=2)
canvas.create_text(630,22.5, text='Help', font='chiller 15 bold')
canvas.bind("<Button-1>", Help)
canvas.pack()
window.mainloop()
main()
首先:您应该将answer = eval(str(rand1) + operation + str(rand2))
替换为answer = (rand1 + rand2) if operation == "+" else (rand1 - rand2)
第二:你在显示的最后一行有count = count + 5
,但计数不是全局的,所以值只是为函数存储,并在函数运行后删除,所以你想要改变的计数保持不变。只要把, count
加到global userint
。
第三:你应该多做一些注释。
第四:你不应该使用全局变量。我的方法是创建一个类,你可以从中创建一个对象;你可以使用self来存储你的数据。然后将对象的函数赋给tkinter