Home > database >  what do need to fix for this answer check to work?
what do need to fix for this answer check to work?

Time:03-26

im a newbie trying to make a random math question generator and the numbers i give the system will output to the question area but when i give the right answer (e.x. 3 times 3 is 9) the system wont see the answer as right.enter image description here

iv tried about 15 different iterations of the code you see here and this is what closest to working

import random
from tkinter import *

na = random.randint(1, 3)
nb = random.randint(1, 3)
txtstring = ("what is", na,"*", nb,"?")

root = Tk()
root.geometry("300x320")
root.title(" Q&A ")
 
def Take_input():
    INPUT = inputtxt.get("1.0", "end-1c")
    print(INPUT)
    if(INPUT == na * nb):
        Output.insert(END, 'Correct')
    else:
        Output.insert(END, "Wrong answer")
     
l = Label(text = txtstring)
inputtxt = Text(root, height = 10,
                width = 25,
                bg = "light yellow")
 
Output = Text(root, height = 5,
              width = 25,
              bg = "light cyan")
 
Display = Button(root, height = 2,
                 width = 20,
                 text ="Show",
                 command = lambda:Take_input())
 
l.pack()
inputtxt.pack()
Display.pack()
Output.pack()
 
mainloop()

CodePudding user response:

inputtxt.get returns a string, but na * nb is an int. So in your example, you're comparing the string "6" against the integer 6. You need to convert INPUT to an integer, otherwise the comparison against na * nb will always fail. E.g.,

if(int(INPUT) == na * nb):

You can improve this code further by handling non-numeric inputs appropriately, but this is the root of the problem.

  • Related