Python获取用户输入等于答案并显示正确或

2024-04-25 21:59:23 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我去找一个熟悉编程的人寻求帮助,但找不到解决方法,我搜索了如何尝试,但我不明白他们的意思,我正在做一个小程序,它教简单的加法和减法,我对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()

Tags: andtexteventdefcreatebuttonwindowwidth
1条回答
网友
1楼 · 发布于 2024-04-25 21:59:23

首先:应该用answer = (rand1 + rand2) if operation == "+" else (rand1 - rand2)替换answer = eval(str(rand1) + operation + str(rand2))

第二:在显示的最后一行中有count = count + 5,但是count不是全局的,因此该值只是为函数存储的,并且在函数运行后get被删除,因此要更改的count保持不变。只需将, count添加到global userint。你知道吗

第三:你应该多提些意见。你知道吗

第四:你不应该使用全局变量。我的方法是创建一个类,从中可以创建一个对象;在这里可以使用self来存储数据。然后把对象的函数给tkinter。你知道吗

相关问题 更多 >