在 python 上检查变量是否为整数

checking if a variable is an integer or not on python

我正在使用 tkinter 进行数学测试。我有 4 个条目允许用户输入问题的答案。答案必须是整数格式,否则会出现很长的错误。我希望我的程序检查输入的值是整数还是 not.and 然后如果它不是整数则打开一个消息框告诉用户检查答案。

这是我的代码:(这是一个很长的代码,因为我不知道如何缩短它,我不是程序员)

from tkinter import*
from tkinter import messagebox
from random import*




n1= randint(1,6)
n2= randint(1,9)
ques1 = n1, "x", n2, "="
c1= n1*n2

n1= randint(8,15)
n2= randint(1,7)
ques2 = n1, "-", n2, "="
c2= n1-n2

n1= randint(1,10)
n2= randint(5,15)
ques3 = n1, "+", n2, "="
c3= n1+n2

n1= randint(5,12)
n2= randint(1,10)
ques4 = n1, "x", n2, "="
c4= n1*n2

#window
window = Tk()
window.geometry("280x450")
window.title("quiz")
window.configure(background='yellow')

def checkthrough():
    if ans1.get() == '':
        messagebox.showinfo("error", "check again ")
    elif ans2.get() == '':
        messagebox.showinfo("error", "check again ")
    elif ans3.get() == '':
        messagebox.showinfo("error", "check again ")
    elif ans4.get() == '':
        messagebox.showinfo("error", "check again ")
    else:
        save()
    #this is where i tried to check if it's an integer or not
    try:
        ans1 == int()
    except:
        messagebox.showinfo("error", "check again ")


def save():
    score = 0
    if c1==int(ans1.get()): 
        score= score + 1
    if c2==int(ans2.get()):
        score = score+1
    if c3==int(ans3.get()):
        score = score+1
    if c4==int(ans4.get()):
        score = score+1
    fscore.set(score)



def savetofile():
    result = result ="\n "+ namestudent.get() + "            " + fscore.get()+"/4"
    messagebox.showinfo("results", "your results been saved successfuly")
    if int(year.get())==1:
        f = open('results C1.txt', 'a')
        f.write(result)
        f.close()
    if int(year.get())==2:
        f = open('results C2.txt', 'a')
        f.write(result)
        f.close()
    if int(year.get())==3:
        f = open('results C3.txt', 'a')
        f.write(result)
        f.close()


#frame
frame = Frame(window)
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)


#variables
namestudent = StringVar()
ans1 = StringVar()
ans2 = StringVar()
ans3 = StringVar()
ans4 = StringVar()
fscore = StringVar()
year = StringVar()

#labels
name = Label(window, text = "type your name:")
name.grid(row= 6, column = 0)

yr = Label(window, text = "type your class:")
yr.grid(row= 7, column = 0)

q1 = Label(window,text = ques1, height = 3, bg = 'yellow')
q1.grid(row = 1,column=0)

q2 = Label(window,text = ques2, height = 3, bg = 'yellow')
q2.grid(row = 2,column=0)

q3 = Label(window,text = ques3, height = 3, bg = 'yellow')
q3.grid(row = 3,column=0)

q4 = Label(window,text = ques4, height = 3, bg = 'yellow')
q4.grid(row = 4,column=0)

#entrys
name_entry= Entry(window, textvariable= namestudent)
name_entry.grid(row = 6, column=1)

yr_entry= Entry(window, textvariable= year)
yr_entry.grid(row = 7, column=1)

q1_entry = Entry(window, width = 6, textvariable = ans1)
q1_entry.grid(row = 1,column=1)

q2_entry = Entry(window, width = 6, textvariable = ans2)
q2_entry.grid(row = 2,column=1)

q3_entry = Entry(window, width = 6, textvariable = ans3)
q3_entry.grid(row = 3,column=1)

q4_entry = Entry(window, width = 6, textvariable = ans4)
q4_entry.grid(row = 4,column=1)

#buttons

finish = Button(window, width = 5, text = "finish",command= checkthrough)
finish.grid(row = 5,column=0)

finalS_label = Label(window, textvariable=fscore)
finalS_label.grid(row=5, column=1)

saving = Button(window, width = 5, text = "save", command= savetofile)
saving.grid(row= 8, column=0)


window.mainloop()

我阅读了其他一些 post 关于同一问题的文章,我尝试在我的代码中使用他们的答案,但我仍然遇到同样的错误。

谢谢。

为了检查字符串是否可整数转换,只需尝试转换它并检查异常。

try:
    integer_result = int(string_result)
except ValueError:
    print("not a valid integer")
else:
    print("value is", integer_result)

要在您的案例中执行此操作,您需要从 ans1 &c 中提取值。事先,然后将原始字符串传递给 int().