Skip to content
Advertisement

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. So I set global bool variables, which are at and dotcom for email validity, but every time I input a valid email format. the bools are still set to false. This is my function for checking the email validity

JavaScript

These are my GUI widgets used for email validation

JavaScript

Every key release after inputting @ and .com still prints out at and dotcom as false when I tried debugging the cause of the problem. 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

Advertisement

Answer

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. Also you overwrite every True statement, because you don’t check if it is already True. e.g. if you already found an ‘@’ you still check for it afterwards and set at to False again.

I have two solutions for you my friend. One is a cleaned up version of your code:

JavaScript

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.

An alternative would be to use ‘re’ to check for a regex:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement