0

I have tried to make a 'password checker' program that checks if an entered variable contains specific characters. I will need it to check thee things: uppercase, lowercase and number. There are 3 things that the program will hopefully output, and these are:

  • Weak, for passwords with only upper OR only lower OR only number

  • Medium, for passwords that have upper and lower, OR upper and number, OR lower and number

  • Strong, for passwords that contain upper, lower and number at the same time.

This is my code:

if EnteredPassword.isupper() or EnteredPassword.islower() or EnteredPassword.isdigit():
    print ("Your password is weak")
elif EnteredPassword.isupper()and EnteredPassword.islower():
    print ("Your password is medium")
elif EnteredPassword.isupper() and EnteredPassword.isdigit():
    print ("Your password is medium")
elif EnteredPassword.islower() and EnteredPassword.isdigit():
    print ("Your password is medium")
elif EnteredPassword.isupper() and EnteredPassword.islower() and EnteredPassword.isdigit():
    print ("Your password is strong")
else:
    print ("That password should not have been accepted")
    quit()

However, when the program is run, and for example I have put in UPPERLOWER6 the program skips to the else statement. If I put something that just contains UPPER etc., that is the only one that works and comes up with your password is weak

If there is anything wrong with the code I cannot see, please point it out. I have been re-directed to other questions but they are too complicated for me and people would know I have copied is, which is not allowed.

Many thanks!

Jair López
  • 650
  • 1
  • 5
  • 16
  • 6
    I don't get it. You've asked this [same question](http://stackoverflow.com/questions/37791034/variable-checking-something-not-right) before and you've recieved an answer which explains your problem clearly! Why would you bother asking it again? You haven't even revised your code. It has the exact logical error pointed out there. – SvbZ3r0 Jun 17 '16 at 14:46
  • The code was too complicated – Noob_coding123 Jun 20 '16 at 13:57
  • Well, ask for clarification then! I'm sure there are people willing to help. What exactly is it that you don't understand? I'll update my answer – SvbZ3r0 Jun 20 '16 at 14:38
  • Everything D: I've only just started and the whole thing doesn't make sense – Noob_coding123 Jun 21 '16 at 12:33
  • Start by reading the [docs](https://docs.python.org/2.7/). – SvbZ3r0 Jun 22 '16 at 05:03

5 Answers5

0

you should do something like this:

Password Tester in Python

the functions you are using test the whole string and to make what you want to do is not the right option.

Community
  • 1
  • 1
cpanato
  • 409
  • 5
  • 8
0

Use regex. Its easier.

Your code isn't working the way you want it to because isupper, islower and isdigit all check all the values of the string you feed it. i.e. 'a'.islower() returns True, while 'aA'.is lower() returns False.

So,

if str.isupper() and str.islower():
    #statement

is never executed because one of the conditions is always bound to be False.

Note: isupper and islower ignore any digits if any character is present in the string. i.e. 'a6'.islower() return True, while '6'.islower() and 'a6'.isdigit() return False.

SvbZ3r0
  • 638
  • 4
  • 19
0

isupper / islower is not working same as like isdigit. upper and lower ignores any digits and whitespaces (eg "UPPER6 ".isupper() is True) on the other way digit check if string contains only digits

Anyway 'UPPERLOWER6' matches first condition, so it shouldn't skip to else. You string probably contains something else.

You can still iterate over chars, eg:

flags = [False] * 3

for ch in EnteredPassword:
  flags[0] = flags[0] or ch.islower()
  flags[1] = flags[1] or ch.isupper()
  flags[2] = flags[2] or ch.isdigit()

strength = sum(flags)
print("Your password is {}".format(
  ["not accepted", "weak", "medium", "strong"][strength])
)
farincz
  • 4,943
  • 1
  • 28
  • 38
0

You r doing it wrong way as you can use isupper or islower functions only on characters and not on whole string. I would do something like this:

def containsOnlyUpper(s):
    for c in s:
        if(not c.isupper()):
            return False
    return True

def containsOnlyLower(s):
    for c in s:
        if(not c.islower()):
            return False
    return True

def containsOnlyNumber(s):
    for c in s:
        if(not c.isdigit()):
            return False
    return True

def containsUpperAndLower(s):
    hasUpper = False
    hasLower = False
    for c in s:
        if (c.islower()):
            hasLower = True
        if (c.isupper()):
            hasUpper = True

    if(hasUpper and hasLower):
        return True
    else:
        return False


def containsUpperAndNumber(s):
    hasUpper = False
    hasNumber = False
    for c in s:
        if (c.isupper()):
            hasUpper = True
        if (c.isdigit()):
            hasNumber = True

    if(hasUpper and hasNumber):
        return True
    else:
        return False

def containsLowerAndNumber(s):
    hasLower = False
    hasNumber = False
    for c in s:
        if (c.islower()):
            hasLower = True
        if (c.isdigit()):
            hasNumber = True

    if(hasLower and hasNumber):
        return True
    else:
        return False

def isWeakPassword(s):
    if(containsOnlyUpper(s) or containsOnlyLower(s) or containsOnlyNumber(s)):
        return True
    return False

def isMediumPassword(s):
    if(containsUpperAndLower(s) or containsUpperAndNumber(s) or containsLowerAndNumber(s)):
        return True
    return False

def isStrongPassword(s):
    hasUpper = False
    hasLower = False
    hasNumber = False
    for c in s:
        if (c.isupper()):
            hasUpper = True
        if (c.islower()):
            hasLower = True
        if (c.isdigit()):
            hasNumber = True

    if (hasLower and hasUpper and hasNumber):
        return True
    else:
        return False

password = "UPPERLOWER6"

if(isWeakPassword(password)):
    print "weak"
elif(isMediumPassword(password)):
    print "medium"
elif(isStrongPassword(password)):
    print "strong"
else:
    print "none"
Teemo
  • 449
  • 6
  • 23
0

I think you could set variables as flags, then check the letter and recording the number of occurrences in input one by one.

agnewee
  • 100
  • 7