2
while True:
    profilePassword = input("Password: ")
    if profilePassword == "":
        print("Your password can't be blank!")
        continue
    else:
        pass
    for n in profilePassword:
        if n == " ":
            print("Your password can't contain spaces!")
            break
        elif n not in "1234567890":
            print("Your password has to contain at least one number!")
            break
        elif n not in "abcdefghijklmnopqrstuvwxyz":
            print("Your password has to contain at least one lowercase letter!")
            break
        elif n not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
            print("Your password has to contain at least one uppercase letter!")
            break
        else:
            continue

So I am trying to create a program that checks if your password is correct. For some reason "not in" doesn't behave very well. If I write, for example, asd123, it will say that the password has to contain at least one number, which it has. Why is that and how can I fix it?

ppasler
  • 3,579
  • 5
  • 31
  • 51
NemPlayer
  • 786
  • 1
  • 11
  • 19
  • I would take a step back and talk yourself through the logic flow you're attempting. The first thing you should notice is that your logic flow is never going to get past `elif n not in "1234567890"` unless the first character in `profilePassword` is a number. You'll run into similar issues for the rest of the `elif` statements. – sco1 Feb 26 '17 at 11:18
  • You have it backwards. Your code asserts that __every__ character is not a space, but is a number __and__ a word character. You should be checking if there _is_ a character that's a number/word character, not if there's a character that _isn't_. – Aran-Fey Feb 26 '17 at 11:18
  • Oh, yes! Thanks for your help. – NemPlayer Feb 26 '17 at 11:25

2 Answers2

1

As comments said too, you are checking every char for being a number (if it is, the second elif fails, because you check if it's a character). This is not what you want to do, you just want to make sure the whole string contains a number.

You may use regex for this:

if re.search(regex, password):
    # regex found something
else:
    # no match

Personally I think it's better to tell the user all the things that failed (not juts the first fail).

import re

def setPassword(profilePassword):
    msg = ""
    if re.search(r' ', profilePassword):
        msg += "* Your password can't contain spaces!\n"

    if not re.search(r'\d', profilePassword):
        msg += "* Your password has to contain at least one number!\n"

    if not re.search(r'[a-z]', profilePassword):
        msg += "* Your password has to contain at least one lowercase letter!\n"

    if not re.search(r'[A-Z]', profilePassword):
        msg += "* Your password has to contain at least one uppercase letter!\n"

    if msg == "":
        print("Setting password was successfull")
    else:
        print("Setting password was not successfull due to following reasons:")
        print(msg)



setPassword("")
setPassword(" ")
setPassword("a")
setPassword("A")
setPassword("1")

setPassword("asd123")

setPassword("aA1")

while True:
    profilePassword = input("Password: ")
    if profilePassword == "":
        print("Your password can't be blank!")
        continue
    else:
        pass
    setPassword(profilePassword)

Also found this while searching: Checking the strength of a password (how to check conditions)

Community
  • 1
  • 1
ppasler
  • 3,579
  • 5
  • 31
  • 51
1

Don't do loops. Convert your password to set and check intersections, along the following lines:

digits = set("0123456789")
pw = set("password")
if not digits.intersection(pw):
     # no digits in password 
avysk
  • 1,973
  • 12
  • 18