Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If statement not detecting string same as other string

import re
def click(event):
    username = username_entry.get()
    password = password_entry.get()
    print(f"Clicked, User = {username}")
    expression = r'(.*).....$'  # Match any characters before the last 5 characters
    masked_password = re.sub(expression, r'\1*****', password)  # Substitute last 5 characters with '*'
    print(f"Masked Password = {masked_password}")
    if password.lower() == str('password'):
        print("Password: GOOD")
        print(f"'{password.lower()}' = 'password'?")
        if any(char.isdigit() for char in password):
            print("Int: GOOD")
            if len(password) >= 8:
                print("Length: GOOD")
            else:
                print("Length: BAD")
        else:
            print("Int: BAD")
    else:
        print("Please fill out all the required fields")

Result:

Clicked, User = Username
Masked Password = Pas*****
Password: GOOD
'password' = 'password'?
Int: BAD

The password seems to be "password", so why isnt it detecting that?

I have no idea why this isnt working, it should say "Please fill out all the required fields"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Two issues:
1)
Change

if password.lower() == str('password'): 

to

if password.lower() != str('password'):

This is you main problem, the code goes in the wrong direction due to this

  1. Invert the condition or the code inside the statement where you check for any digit:

The any() function returns True if any element of an iterable is True. If not, it returns False. As none of the char is digit in password, you will go to the else statement

Should work like this:

 if any(char.isdigit() for char in password):
     print("Int: BAD")
 else:
     print("Int: GOOD")
     if len(password) >= 8:
         print("Length: GOOD")
     else:
         print("Length: BAD")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading