简体   繁体   English

为什么我的elif语句不会执行,但我的if语句将在Python中执行?

[英]Why won't my elif statement execute but my if statement will in Python?

I wrote a simple age verification in python to where if the user enters a year less than 2000, it will say welcome. 我在python中写了一个简单的年龄验证,如果用户输入的年份少于2000,那就说欢迎。 However, if the user enters a year greater than 2000, it should redirect them to another site. 但是,如果用户输入的年份大于2000,则应将其重定向到另一个站点。

My code works but only executes only the if statement not the elif no matter what year I input. 我的代码有效,但无论我输入什么年份,​​只执行if语句而不是elif

Here is my code: 这是我的代码:

from Tkinter import *
import webbrowser
import tkMessageBox


url = 'google.com'
root = Tk()
frame = Frame(root, width=100, height=100)
frame.pack()
L1 = Label(root, text = "Month")
L1.pack(side = LEFT)
E1 = Entry(root,bd=5)
E1.pack(side=LEFT)
L2 = Label(root, text = "Day")
L2.pack(side =LEFT)
E2 = Entry(root, bd= 5)
E2.pack(side = LEFT)
L3 = Label(root, text = "Year")
L3.pack(side = LEFT)
E3 = Entry(root, bd = 5)
E3.pack(side = LEFT)

def getdate():
    tkMessageBox.showinfo(title ="Results", message = E1.get() + " "+ E2.get() + " " + E3.get())
    getage()
    root.destroy()
    #tkMessageBox.showinfo(E2.get())
    #tkMessageBox.showinfo(E3.get())
def getage():

    if E3 < 2000:

        tkMessageBox.showinfo(message= "Welcome! ")
    elif E3 > 2000:
        tkMessageBox.showinfo(message="You will be redirected")
        webbrowser.open_new(url)


b1 = Button(root, text = "Submit", width = 25, command = getdate)
b1.pack()


root.mainloop()

You are comparing the Entry() object to an integer. 您正在将Entry()对象与整数进行比较。 In Python 2, numbers are always sorted before other types, so they always register as smaller : 在Python 2中,数字总是其他类型之前排序,因此它们总是注册为更小

>>> object() > 2000
True

You want to get the value from the entry box and convert to an integer before testing: 您想从输入框中获取值并在测试之前转换为整数:

entry = int(E3.get())
if entry > 2000:
     # ...
else:
     # ...

There is no need to do elif here, unless you want the value 2000 to be ignored altogether (your test only works for numbers either greater than, or smaller than, not equal). 除非您希望完全忽略值2000 (您的测试仅适用于大于或小于,不等于的数字),否则不需要在此处执行elif

Entry is the object type. Entry是对象类型。 You want to call the get() function. 你想调用get()函数。 Like this: 像这样:

EntryBox = Entry(root, bd = 5)
E3 = int(EntryBox.get())

Also, note that the elif is completely unnecessary. 另外,请注意, elif完全没必要。 Just use else instead. 只需使用else

if E3 < 2000:
    tkMessageBox.showinfo(message= "Welcome! ")
else:
    tkMessageBox.showinfo(message="You will be redirected")
    webbrowser.open_new(url)

You compare an Entry object to an integer ,the if statement will never be reached because integers are always sorted before objects . Entry对象与integer进行比较,永远不会达到if语句,因为integers总是在objects之前排序。 Look: 看:

>>> a = ['0', 9999, {}, [], False, (), object()]
>>> sorted(a)
[False, 9999, {}, [], <object object at 0xb74fa558>, '0', ()]
>>> 

Use get method to get text from Entry then convert it with int : 使用get方法从Entry获取文本然后使用int转换它:

if int(E3.get()) < 2000:

You can use IntVar() if you want to get integer from Entry without converting: 如果要从Entry中获取整数而不进行转换,则可以使用IntVar()

...
v = IntVar()
E3 = Entry(root, bd = 5, textvariable=v)
...
if E3.get() < 2000:
    tkMessageBox.showinfo(message= "Welcome! ")
else:
    tkMessageBox.showinfo(message="You will be redirected")
    webbrowser.open_new(url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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