Jake Sanchez
Jake Sanchez

Reputation: 25

How to check if a variable has characters that are not allowed in?

I am writing a password program where it will display a score if certain requirements are met. But I am stuck on how to check if some characters are not allowed. If they are not allowed it should notify the reader. Here is my code:

user_password = input("\nEnter your password:")
user_score = 0
user_length = len(user_password)
symbols = "$%^&*()_-+="

if len(user_password)>24:
    print("Your password is too long! It must be between 6 and 24")
elif len(user_password)<6:
    print("Your password is too short! It must be between 6 and 24")
elif len(user_password) >=6 and len(user_password) <= 24:
    lower = sum([int(c.islower()) for c in user_password])
    if lower > 0:
            user_score = user_score + 5
    upper = sum([int(c.isupper()) for c in user_password])
    if upper > 0:
            user_score = user_score + 5
    integer = sum([int(c.isdigit()) for c  in user_password])
    if integer > 0:
            user_score = user_score + 5
    for c in symbols:
            if c in user_password:
                    user_score = user_score + 5
    for c in user_password:
            if c not in symbols:
                    print("Some symbols you entered are not allowed")
                    break

I want it so that the program will end if a symbol is wrongly entered it. However when a wrong symbol is entered it displays the message the amount of times the symbol is entered. Any help will be appreciated.

Upvotes: 2

Views: 68

Answers (2)

Joe Iddon
Joe Iddon

Reputation: 20434

You need to change the last for-loop so that it fails if c in symbols, you had a not in there that was throwing the program off...

for c in user_password:
            if c in symbols:
                    print("Some symbols you entered are not allowed")
                    break

A shorter way to do this with just 2``lines would be to use any:

if any(c in symbols for c in user_password):
            print("Some symbols you entered are not allowed")

As a final note, you should try to keep your indentation width to a constant 4 spaces for readablility. I haven't done this in these snippets as you will be able to test them with your existing code, but ideally you should change them.

Upvotes: 1

alexisdevarennes
alexisdevarennes

Reputation: 5642

Change if c not in symbols: to if c in symbols:

user_password = input("\nEnter your password:")
user_score = 0
user_length = len(user_password)
symbols = "$%^&*()_-+="

if len(user_password)>24:
    print("Your password is too long! It must be between 6 and 24")
elif len(user_password)<6:
    print("Your password is too short! It must be between 6 and 24")
elif len(user_password) >=6 and len(user_password) <= 24:
    lower = sum([int(c.islower()) for c in user_password])
    if lower > 0:
            user_score = user_score + 5
    upper = sum([int(c.isupper()) for c in user_password])
    if upper > 0:
            user_score = user_score + 5
    integer = sum([int(c.isdigit()) for c  in user_password])
    if integer > 0:
            user_score = user_score + 5
    for c in symbols:
            if c in user_password:
                    user_score = user_score + 5
    for c in user_password:
            if c in symbols:
                    print("Some symbols you entered are not allowed")
                    break

Hope this helps and welcome to SO!

Upvotes: 1

Related Questions