需要解决什么问题才能使此答案检查正常工作?

what do need to fix for this answer check to work?

我是一个试图制作随机数学问题生成器的新手,我给系统的数字将输出到问题区域,但是当我给出正确答案时(e.x。3 乘以 3 是 9)系统不会将答案视为正确的。

iv 尝试了大约 15 次不同的代码迭代,这是最接近工作的代码

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

inputtxt.get returns 一个字符串,但是 na * nb 是一个 int。因此,在您的示例中,您将字符串 "6" 与整数 6 进行比较。您需要将 INPUT 转换为整数,否则与 na * nb 的比较将始终失败。例如,

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

您可以通过适当处理 non-numeric 输入来进一步改进此代码,但这是问题的根源。