0

Sorry, I am trying to write a code on python 3.2 for password strength check but it looks like I keep getting error somewhere. Whenever my input is #$%$J, is will go into the if input.upper() is true. How do I prevent this from happening? And my score value is not accurate. coding:

password_user = input("Input password : ")
score = 0
if len(password_user) <=5:
    print("Password is too short! ")
    score = 1
elif password_user == password_user.lower():
    print("Bad Password")
    score = 1
elif password_user == password_user.upper():
    print("Bad Password")
    score = 1
else:
    while score == 0:
        for x in range(33,48):
            ascii_str = chr(x)
            if password_user.find(ascii_str) >= 0:
                score = score + 3
        for y in range(97,123):
            ascii_lower = chr(y)
            if password_user.find(ascii_lower) >= 0:
                score = score + 1
        for w in range(65,91):
            ascii_upper = chr(w)
            if password_user.find(ascii_upper) >= 0:
                score = score + 2
        for z in range(48,58):
            ascii_num = chr(z)
            if password_user.find(ascii_num) >= 0:
                score = score + 2
if score >0 | score <=5:
    print("Weak Password")
    print(score)
elif score > 5 | score < 7:
    print("Medium Password")
    print(score)
elif score >= 7:
    print("Strong Password")
    print(score)
Jordan Iatro
  • 289
  • 3
  • 10
  • 1
    Your question is too broad, try to narrow your question down to a few lines of code you need help with. Include sample data, and what the expected flow/result would be. – Inbar Rose Oct 08 '13 at 12:43
  • Can you specify your error? Maybe i can help. – aIKid Oct 08 '13 at 12:44
  • Consult my [answer to a similar question](http://stackoverflow.com/a/16710035/1561176) – Inbar Rose Oct 08 '13 at 12:47
  • 1
    You have an indentation error on your `while` loop there, by the way. – aIKid Oct 08 '13 at 12:47
  • 1
    That's a very strange method of calculating password strength...where did you get that from? Also, why is the first character of your password irrelevant for the score? – Tim Pietzcker Oct 08 '13 at 12:49
  • By right, by inputing JoRD@N I am suppose to get a score of 12, 4upr case, 1 symbol and 1 lower case. but my output is 9 instead. – Jordan Iatro Oct 08 '13 at 12:56

1 Answers1

1

First of all, it should be, e.g., find(ascii_str) > -1 (so that character number 0 is counted). I'm assuming you mean to give a score bonus at most once for each range of characters. Therefore, something like this is appropriate:

contain = false
for x in range(33,48):
    ascii_str = chr(x)
    if password_user.find(ascii_str) > -1:
        contain = true
if contain:
    score += 3

and similarly for the others.

EDIT: The question does not make clear whether you want each distinct character to increase the score, for each character to increase the score, or only for each type of character to increase the score. Given your metric of any score greater than 6 being strong, anything that doesn't return "Password is too short" or "Bad Password" will be strong (it'll be at least 6 characters, with at least one capital letter, already giving 7 points) unless you have many repeated letters. If you really want each new character to increase the score, you should change the metric (maybe 12 or more is a strong password?), although in my opinion it makes more sense to just increase the score for each type of character contained, and then increase the score by the length, or something like that.

  • He wants to count the number of uppercase, lowercase letters, symbols and such. Not finding it. – aIKid Oct 08 '13 at 13:22
  • The code he has doesn't count them; it counts the number of distinct ones. I think it makes more sense to just find them, given his metric of any score >= 7 is strong. By that metric anything that doesn't return "Password is too short" or "bad password" will be strong (it'll be at least 6 characters, with at least one capital letter) unless he has many repeated letters. It really depends on what exactly he wants for the metric, I guess. Does he want each distinct letter to increase the score, or only one increase, or each (not necessarily distinct) letter to increase the score? – antimonyarsenide Oct 09 '13 at 16:05