如何使用 Python 中的 range() 函数和布尔值来检查字符串是否包含特定字符?

How to check check if a string contains a specific character(s) using range() functions and bools in Python?

我正在尝试在 Python GUI 中检查用户的电子邮件在每次密钥发布后是否有效。所以我设置了全局 bool 变量,它们是 atdotcom 用于电子邮件有效性,但每次我输入有效的电子邮件格式。 bools 仍然设置为 false。 这是我检查电子邮件有效性的功能

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")

这些是我用于电子邮件验证的 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)

当我尝试调试问题的原因时,输入 @ 和 .com 后的每个键释放仍然打印出 atdotcom 作为 false。我提前搜索了字符串、范围函数、子字符串和条件,但我仍然不知道为什么我的 bool 仍在输出 false,这意味着只有 else 条件是唯一起作用的条件

目前你正在做一些奇怪的事情。您遍历电子邮件的长度,但随后不使用迭代器来检查值。您还覆盖了每个 True 语句,因为您不检查它是否已经为 True。例如如果您已经找到一个“@”,您之后仍会检查它并再次将 at 设置为 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")

问题在于,当您删除密钥时,您仍然需要在和 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")