简体   繁体   中英

Python- Why does it say password denied even when I enter a password that should work?


userpass= input('Enter a password with at least one uppercase letter, one lowercase letter, and one number: ')
uppercounter=0
lowercounter=0
numbercounter=0
for i in range(len(userpass)):


    if userpass[i].isupper():
        uppercounter=uppercounter+1
        print(uppercounter)
        if uppercounter > 0:
            print("working")
    else:
        print('Password Denied')
        raise SystemExit(0)

    if userpass[i].islower():
        lowercounter=lowercounter+1
        print(lowercounter)
        if lowercounter > 0:
            print('working')
    else:
        print('Password Denied')
        raise SystemExit(0)

    if userpass[i].isnumeric():
        numbercounter=numbercounter+1
        print(numbercounter)
        if numbercounter > 0:
            print("working")
            print("Password Accepted")
    else:
        print('Password Denied')
        raise SystemExit(0)

I'm trying to make a program for a password that must contain one uppercase letter, one lowercase letter, and one number. But the if statements don't seem to be working properly and whenever I enter a password like “Py11”, it says password denied.

For every char of the password you are checking againt all three conditions: lets say you typed in: "hi"

for the first loop you'll be in letter "h" if it is upper (which is not) your else you be executed running the password denied part (thats the problem)

In order to acomplish what you want I suggest checking like this:

if condition1:
    pass #add here the counting you are making
elif condition2:
    pass #add here the counting you are making
elif condition3:
    pass #add here the counting you are making
# [...]
else:
    print("password denied")

and for the conditions you check for upper, lower, numeric and if you want to allow special chars you need to check for them also

Other way you could do it:

userpass= input('Enter a password with at least one uppercase letter, one lowercase letter, and one number: ')
uppercounter=0
lowercounter=0
numbercounter=0
for i in range(len(userpass)):
    if userpass[i].isupper():
        uppercounter=uppercounter+1
    if userpass[i].islower():
        lowercounter=lowercounter+1
    if userpass[i].isnumeric():
        numbercounter=numbercounter+1
if uppercounter == 0 or lowercounter == 0 or numbercounter == 0:
    print("Password denied")
    raise SystemExit(0)

this way you don't have to check against other special chars

Because your logic is wrong. So first, you're entering the Py11 password and it enters the first if statement.

Your loop takes first value is P from Py11 . When it goes to second if statement, it immediately goes to else statement of second if statement because the value is still P .

I tried not to modify your code so much but you could try this way:

userpass = input(
    'Enter a password with at least one uppercase letter, one lowercase letter, and one number: ')
uppercounter = 0
lowercounter = 0
numbercounter = 0
for i in range(len(userpass)):
    print(userpass[i])
    if userpass[i].isupper():
        uppercounter = uppercounter+1
        print(uppercounter)
        if uppercounter > 0:
            print("working")
    elif userpass[i].islower():
        lowercounter = lowercounter+1
        print(lowercounter)
        if lowercounter > 0:
            print('working')
    elif userpass[i].isnumeric():
        numbercounter = numbercounter+1
        print(numbercounter)
        if numbercounter > 0:
            print("working")

if uppercounter <= 0 or lowercounter <= 0 or numbercounter <= 0:
    print('Password Denied')
    raise SystemExit(0)
else:
    print("Password Accepted")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM