简体   繁体   中英

Python - Getting User input equalling answer and displaying correct or incorrect

So I've gone to a person who is familiar in programing for help but couldn't find the solution, I've searched how to try and do it but i couldn't understand what they meant, I'm making a little program where it teaches simple addition and subtraction and I'm quite new to python. So how do i get this "user input equals answer then display correct or incorrect" to work cause it is just looping and looping the questions because i want 5 questions only. Don't judge i know it's messy but code is below. please help.

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()

First: You should replace answer = eval(str(rand1) + operation + str(rand2)) with answer = (rand1 + rand2) if operation == "+" else (rand1 - rand2)

Second: You have count = count + 5 in the last line of display, but count isn't global, so the value is just stored for the function and get's deleted after the function has run, so the count you want to change remains the same. Just add , count to global userint .

Third: You should make some more comments.

Fourth: You shouldn't use global variables. My approach would be to create a class, from wich you would create an object; where you can use self to store your data. And then you give the functions of the object to tkinter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM