![](/img/trans.png)
[英]How to check if a string contains a specific character or not in python
[英]How to check check if a string contains a specific character(s) using range() functions and bools in Python?
如果用戶的 email 在每次密鑰釋放后都有效,我正在嘗試檢查 Python GUI。 所以我設置了全局 bool 變量,它們是at
和dotcom
,用於 email 有效性,但每次我輸入一個有效的 email 格式。 布爾值仍設置為 false。 這是我的 function 用於檢查 email 的有效性
def keyup_email(e):
global at
global dotcom
for x in range(len(custemail.get())): #gets email length after every key release
if custemail.get()[-1] == "@" and x > 3: #check if inputted email contains "@" after typing it
at = True
print("at is" + str(at))
else:
c_email_format.config(text="Bad email")
at = False
print("at is" + str(at))
if x > 5 and custemail.get()[-4:] == ".com" and at == True: #check if inputted email's last 4 characters is ".com"
dotcom = True
print("dotcom is" + str(dotcom))
else:
c_email_format.config(text="Bad email")
dotcom = False
print("dotcom is" + str(dotcom))
if at == True and dotcom == True:
c_email_format.config(text="Good email")
這些是我用於 email 驗證的 GUI 小部件
c_email = Label(window, text="Customer Email:", width=15, height=1, bg="yellow")
c_email.grid(column=1, row=4)
custemail = StringVar()
custemail = Entry(window, textvariable=custemail)
custemail.grid(column=2, row=4)
custemail.bind("<KeyRelease>", keyup_email)
c_email_format = Label(window, text="[a-z]@[a-z].com", width=15, height=1, bg="yellow")
c_email_format.grid(column=3, row=4)
當我嘗試調試問題的原因時,輸入@ and.com 后的每個鍵釋放仍然at
和dotcom
上打印為false
。 我在字符串、范圍函數、子字符串和條件上進行了搜索,但我仍然不知道為什么我的布爾值仍然輸出false
,這意味着只有else
條件是唯一起作用的條件
目前你正在做一些奇怪的事情。 您迭代 email 的長度,但隨后您不使用迭代器來檢查值。 您還覆蓋了每個 True 語句,因為您不檢查它是否已經為 True。 例如,如果您已經找到一個“@”,您仍然會在之后檢查它並再次設置為 False。
我有兩個解決方案給你,我的朋友。 一個是您的代碼的清理版本:
def keyup_email(e):
global at
global dotcom
email = custemail.get()
for x in range(len(email)): #iterate over the whole email
if not at: #if we found an '@' already, we don't want to check again to overwrite it
at = email[x] == "@" and x > 3 #we can just set at to the result of the boolean operation
print("at is" + str(at))
if not dotcom: #if we found 'dotcom' already, we don't want to overwrite it
dotcom = x > 5 and email[x:] == ".com" and at #again, we can just set dotcom to the boolean value
print("dotcom is" + str(dotcom))
if at and dotcom: #as at and dotcom are boolean we don't have to do an '== True'-check
c_email_format.config(text="Good email")
else:
c_email_format.config(text="Bad email")
問題是,當您刪除密鑰時,您仍然需要重置 at 和 dotcom,因為我們需要再次檢查所有內容。
另一種方法是使用“re”來檢查正則表達式:
import re
def keyup_email(e):
contains_at = re.search("@", custemail.get()) #check whether email contains an '@' symbol
ends_with_dotcom = re.search('\.com$', custemail.get()) #check whether email ends with '.com'
if(contains_at != None and ends_with_dotcom != None): # if neither contains_at nor ends_with_dotcom are None, we found a valid email
c_email_format.config(text="Good Email")
else:
c_email_format.config(text="Bad Email")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.