所以我去了一个熟悉编程帮助但却找不到解决方案的人,我已经搜索了如何尝试这样做,但我无法理解他们的意思是,我制作了一个小程序,它教的是简单的加法和减法,而且我对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()
答案 0 :(得分:0)
首先:您应该将answer = eval(str(rand1) + operation + str(rand2))
替换为answer = (rand1 + rand2) if operation == "+" else (rand1 - rand2)
第二:你在显示的最后一行有count = count + 5
,但是count不是全局的,所以只为函数存储了值,并在函数运行后删除了,所以你想要的计数变化仍然是一样的。只需将, count
添加到global userint
。
第三:你应该多做一些评论。
第四:你不应该使用全局变量。我的方法是创建一个类,从中创建一个对象;您可以使用self来存储数据。然后你将对象的功能赋予tkinter。