我正在尝试验证用户输入,但我的问题是,如果第一个IF
语句为true,则使用当前代码,则不会查找其余的ELIF
语句。这意味着如果第一个输入框中有错误,其余的输入框都没有得到验证,这是我的代码:
if (len(MemberID)) > 4:
self.MemberIDError.configure(fg = "red")
elif (len(Postcode)) > 7:
self.PostcodeError.configure(fg = "red")
elif (len(Age)) > 3:
self.AgeError.configure(fg = "red")
elif (len(Mass)) > 3:
self.MassError.configure(fg = "red")
elif (Name == '') or (MemberID == '') or (Postcode == '') or (Age == '') or (Gender == '') or (Mass == ''):
self.CreateError.configure(fg = "red")
conn.commit()
else:
List = [Name, MemberID, Postcode, Age, Gender, Mass]
cursor.execute("INSERT INTO memb(Name, MemberID, Postcode, Age, Gender, Mass) VALUES (?,?,?,?,?,?)",(Name, MemberID, Postcode, Age, Gender, Mass))
conn.commit()
或者,如果我将“ELIF”语句更改为“IF”语句,则仍会将错误的用户输入添加到数据库中。我该怎么做才能确保正确验证用户输入,以及为什么没有使用“ELIF”语句。
答案 0 :(得分:0)
只需输入多个IF
语句,并跟踪它是否有效:
valid=True
if (len(MemberID)) > 4:
self.MemberIDError.configure(fg = "red")
valid=False
if (len(Postcode)) > 7:
self.PostcodeError.configure(fg = "red")
valid=False
if (len(Age)) > 3:
self.AgeError.configure(fg = "red")
valid=False
if (len(Mass)) > 3:
self.MassError.configure(fg = "red")
valid=False
if (Name == '') or (MemberID == '') or (Postcode == '') or (Age == '') or (Gender == '') or (Mass == ''):
valid=False
self.CreateError.configure(fg = "red")
conn.commit()
if valid:
List = [Name, MemberID, Postcode, Age, Gender, Mass]
cursor.execute("INSERT INTO memb(Name, MemberID, Postcode, Age, Gender, Mass) VALUES (?,?,?,?,?,?)",(Name, MemberID, Postcode, Age, Gender, Mass))
conn.commit()
答案 1 :(得分:0)
当您使用if
语句编写elif
语句时,您正在做的是告诉程序检查第一个if
语句,如果有效,则不要检查其他elif
语句以下陈述。这将按顺序检查,直到其中一个if/elif
有效或者您的if/elif
语句用完为止。
如果您希望检查每个参数而不管先前的if
语句是否有效,您想要做什么,那么您需要将其写为多个if语句。
这样的事情:
if (len(MemberID)) > 4:
self.MemberIDError.configure(fg = "red")
if (len(Postcode)) > 7:
self.PostcodeError.configure(fg = "red")
if (len(Age)) > 3:
self.AgeError.configure(fg = "red")
if (len(Mass)) > 3:
self.MassError.configure(fg = "red")
if (Name == '') or (MemberID == '') or (Postcode == '') or (Age == '') or (Gender == '') or (Mass == ''):
self.CreateError.configure(fg = "red")
conn.commit()
if:
List = [Name, MemberID, Postcode, Age, Gender, Mass]
cursor.execute("INSERT INTO memb(Name, MemberID, Postcode, Age, Gender, Mass) VALUES (?,?,?,?,?,?)",(Name, MemberID, Postcode, Age, Gender, Mass))
conn.commit()
答案 2 :(得分:-1)
你的问题是elif基本上说如果前一个if不起作用,并且它满足第二个条件,但由于前一个条件确实有效,它永远不会激活第二个比较。这应该有效:
x = 0
y = 0
While x == 0:
if (len(MemberID)) > 4:
self.MemberIDError.configure(fg = "red")
y = 1
if (len(Postcode)) > 7:
self.PostcodeError.configure(fg = "red")
y = 1
if (len(Age)) > 3:
self.AgeError.configure(fg = "red")
y = 1
if (len(Mass)) > 3:
self.MassError.configure(fg = "red")
y = 1
if (Name == '') or (MemberID == '') or (Postcode == '') or (Age == '') or (Gender == '') or (Mass == ''):
self.CreateError.configure(fg = "red")
conn.commit()
y = 1
if y != 0:
print "Fix changes in red"
break
else:
List = [Name, MemberID, Postcode, Age, Gender, Mass]
cursor.execute("INSERT INTO memb(Name, MemberID, Postcode, Age, Gender, Mass) VALUES (?,?,?,?,?,?)",(Name, MemberID, Postcode, Age, Gender, Mass))
conn.commit()
break
[编辑]所以我想我知道你正在尝试做什么。您正在尝试输入验证。我所做的就是把一切都搞定了。如果这是一个错误,我设置一个新变量的值" y"到1,代码将继续检查所有内容,但是当它到达底部时,即使输入的内容不正确,它也会告诉您将内容修复为红色,并打破循环。或者,如果没有任何内容被破坏,则将该成员添加到数据库中。