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

Why does my if statement return the wrong prompt?

I am sure the answer is right in front of my face but I can’t seem to figure out how to fix the return on my first if statement when I input the right conditions.

create_password = input("Enter password here: ")

if len(create_password) > 6 and create_password.isdigit() > 0:
    print("Your account is ready!")
elif len(create_password) < 6 and create_password.isdigit() < 1:
    print("Password must be more than 6 characters and must include a number")
elif len(create_password) > 6 and create_password.isdigit() == 0:
    print("Password must include a number")
else:
    print("Your password sucks")

Let’s say I enter elephant100, I am trying to get the prompt to be "Your account is ready!". But to my dismay, it prints "Password must include a number" and I cannot figure out why. My other conditions match the right input but that is the only one that does not work.

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 :

.isdigit() method returns True if all the characters are digits, otherwise False. Hence it returns False in this case since your string contains letters like e, l, p etc. So the statement print("Your account is ready!") will never be executed.

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