简体   繁体   English

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

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

I'm trying to check in Python GUI if the user's email is valid after every key release.如果用户的 email 在每次密钥释放后都有效,我正在尝试检查 Python GUI。 So I set global bool variables, which are at and dotcom for email validity, but every time I input a valid email format.所以我设置了全局 bool 变量,它们是atdotcom ,用于 email 有效性,但每次我输入一个有效的 email 格式。 the bools are still set to false.布尔值仍设置为 false。 This is my function for checking the email validity这是我的 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")

These are my GUI widgets used for email validation这些是我用于 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)

Every key release after inputting @ and.com still prints out at and dotcom as false when I tried debugging the cause of the problem.当我尝试调试问题的原因时,输入@ and.com 后的每个键释放仍然atdotcom上打印为false I searched ahead on strings, range functions, substrings, and conditionals, but I'm still clueless as to why my bools are still outputting false , which means that only else condition is the only functioning conditional我在字符串、范围函数、子字符串和条件上进行了搜索,但我仍然不知道为什么我的布尔值仍然输出false ,这意味着只有else条件是唯一起作用的条件

Currently you are doing some strange things.目前你正在做一些奇怪的事情。 You iterate over the length of the email, but then you don't use the iterator to check the values.您迭代 email 的长度,但随后您不使用迭代器来检查值。 Also you overwrite every True statement, because you don't check if it is already True.您还覆盖了每个 True 语句,因为您不检查它是否已经为 True。 eg if you already found an '@' you still check for it afterwards and set at to False again.例如,如果您已经找到一个“@”,您仍然会在之后检查它并再次设置为 False。

I have two solutions for you my friend.我有两个解决方案给你,我的朋友。 One is a cleaned up version of your code:一个是您的代码的清理版本:

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

Problem with this is, that you still need to reset at and dotcom when you delete a key, because then we need to check everything again.问题是,当您删除密钥时,您仍然需要重置 at 和 dotcom,因为我们需要再次检查所有内容。

An alternative would be to use 're' to check for a regex:另一种方法是使用“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.

相关问题 如何在 python 中检查字符串是否包含特定字符 - How to check if a string contains a specific character or not in python Python:如何检查字符串是否包含特定范围内的数字 - Python: How to check if the string contains number from a specific range 如何在Python中检查列表是否包含特定字符串 - how to check if a list contains a specific string or not in python 如何检查python字符串列表在字符串的最后一个字母中是否包含特定字符? - How to check if a python string list contains a specific character in the last letter of a string? 如何检查字符是否在 Python 的字符串中的特定 position 中? - How to check if a character is in a specific position in a string in Python? Python:如何检查unicode字符串是否包含一个cased字符? - Python: How to check if a unicode string contains a cased character? Python:检查字符串是否包含中文字符? - Python: Check if a string contains chinese character? 检查字符串是否包含所需的字符 position python - check if the string contains the desired Character position python 检查字符串是否在python中包含字符 - check whether a string contains a character in python 检查字符串是否只包含python中的某个字符 - Check if string only contains a certain character in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM