Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

password validation - python

I am trying to make a code to validate a password.

I have the following code:

password = input("Please enter password: ")

length = len(password)
characters = False
digit = False
capital = False

if length > 6:
    characters = True
    # print("Length Good")

for count in password:
    if count.isdigit():
        digit = True
        # print ("Contains digit")

for count in password:
    if count.isupper():
        capital = True
        # print ("Contains a capital")

if characters == False and digit == False and capital == False:
    print("Password is good")
else:
    print("Bad password")

Whatever I enter, it prints "Bad password", any ideas what I have done wrong?

question from:https://stackoverflow.com/questions/65876475/password-validation-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I ran it and entered "test" and it said "Password is good". I suspect you meant to have:

password = input("Please enter password: ")

length = False
digit = False
capital = False

length = len(password)

if length > 6:
    length = True
    #print("Length Good")

for count in password:
    if count.isdigit():
        digit = True
        #print ("Contains digit")

for count in password:
    if count.isupper():
        capital = True
        #print ("Contains a capital")

if length == True and digit == True and capital == True:
    print("Password is good")
else:
    print("Bad password")

because otherwise you are enforcing bad passwords.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...