2

Hey guys I am new to Python and I wrote this password program in Python but the first 'elif' statement doesn't execute for some reason. If i input a password that has less than 5 characters AND has a special character in it, it just says that it's too short whereas it's supposed to say it's too short AND you can't use special characters. Any help would be appreciated.

password = 'Chubz'
count = 3
listA = list("'#$%&\()@*+,-./:;?@[\\]^_`{|}~ \t\n\r\x0b\x0c'")

while count >= 0:
    x = input('What is the pass? ')
    if x in listA:
        print(f" {count} You can't use special characters ")
        count -= 1
    elif x in listA and len(x) < 5:
        print(f"{count} You can't use special characters and the pass is too short")
        count -= 1
    elif len(x) < 5:
        print(f'{count} Pass is too short')
        count -= 1
    elif x != password:
        print(f'{count} Try again')
        count -= 1
    else:
        print('Access Granted')
        break
4
  • x is a string. You want to check if any of characters within x is a special character. To do that, you have to iterate through x. You cannot just do if x in listA Commented Sep 18, 2020 at 4:25
  • Well how do I write the code for that? Commented Sep 18, 2020 at 4:33
  • see my response below. It gives you the details. I have also explained any() function. Commented Sep 18, 2020 at 4:33
  • To learn more about any() use this link. Similar to any(), you also have all() Commented Sep 18, 2020 at 4:40

4 Answers 4

3
if x in listA:
    print(f" {count} You can't use special characters ")
    count -= 1

elif x in listA and len(x) < 5:

There is no way to reach this latter clause: it is a subset of the first one. Anything that qualifies for the elif has already been handled by the if. You need to switch the order of checks.

Sign up to request clarification or add additional context in comments.

2 Comments

So what you are saying is that I need to swap the first 'if' statement with the first 'elif' statement?
At least that much, yes. For the rest, you have to decide which error message you want printed in case the user makes several mistakes in one input.
1

Instead of checking it in the order you have, why don't you check it in the below order. It will ensure the elif gets executed only if the previous condition is not satisfied.

Also, you need to check for each character of x instead of the whole string of x.

To do that you can use any()

any([True for i in x if i in listA])

This will be set to True if any of the characters in x is a special character.

password = 'Chubz'
count = 3
listA = list("'#$%&\()@*+,-./:;?@[\\]^_`{|}~ \t\n\r\x0b\x0c'")

while count >= 0:
    x = input('What is the pass? ')
    if len(x) < 5:
        print(f'{count} Pass is too short')
        count -= 1
    elif any([True for i in x if i in listA]):
        print(f" {count} You can't use special characters ")
        count -= 1
    elif x != password:
        print(f'{count} Try again')
        count -= 1
    else:
        print('Access Granted')
        break

3 Comments

Thanks a bunch, this one worked. I just had to add the any([True for i in x if i in listA]) for 2 of the if statements.
Thanks for the confirmation. If you think this answered your question, please read about what to do when someone answers your question. Upvote the answer if it solved your question.
using the any() twice is costly. If you switch the if statements a bit, you need to do the any only once like I did in my example. Both works.
1

To fix this, just put elif x in listA and len(x) < 5: print(f"{count} You can't use special characters and the pass is too short") count -= 1 Before the first if statement so this would become the if statement instead of elif. The interpreter reads from up to down so it saw that a special character was used to directly printed it.

SO the code would be: if x in listA and len(x) > 5: print(f" {count} You can't use special characters ")

count -= 1

4 Comments

I already tried putting swapping their places but it still didn't work...
It should; are you on the latest version of Python: 3.8.5?
I'm on 3.8.0 but I don't think it matters
x is a string. so PO needs to check for each character of x against the list.
1

The if statement is true and after that it will exit... it will never reach to first elif. you need to add the seconds condition as well in elif.

if x in listA and len(x) > 5::
    print(f" {count} You can't use special characters ")
    count -= 1

Basically if statements work best if they are mutually exclusive.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.