Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...