我是一個新手,試圖制作一個亂數學問題生成器,我給系統的數字將輸出到問題區域,但是當我給出正確的答案(例如 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()
uj5u.com熱心網友回復:
inputtxt.get回傳一個字串,但na * nb它是一個int. 因此,在您的示例中,您將 string"6"與 integer進行比較6。您需要轉換INPUT為整數,否則比較na * nb總是會失敗。例如,
if(int(INPUT) == na * nb):
您可以通過適當地處理非數字輸入來進一步改進此代碼,但這是問題的根源。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/451720.html
