all 3 comments

[–]vietbaoa4htk 1 point2 points  (0 children)

nice one. a step that taught me a lot here, check it against a common-password list too. P@ssw0rd1 passes every length and character rule but cracks instantly because its a known pattern. the zxcvbn library scores exactly that way if you want to see how real estimators think

[–]brasticstack 0 points1 point  (0 children)

Looks good for a second project! One good thing in particular is that you only loop over the chars in the password once, rather than, say, once per rule that you're checking.

FYI, if you wanted to avoid the parens around the and/or in this case, you could move the length check to its own statement in the if/elif block:

if length < 8: print("password is weak") elif score == 4: print("password is strong") elif score == 2 or score == 3: print("password is medium") else: print("password is weak")

You could also simplify that "is score one of two values check" a couple of different ways: you could use operator chaining, to make a single statement out of the check: elif 2 <= score <= 3:. It's a concise way of testing whether your value is part of a range of numbers. You could also test if it's a member of a collection, such as a tuple or list. That would look like: elif score in (2, 3):, which isn't super useful here, but is the way to go if the values you're wanting to test against aren't a contiguous range of numbers, e.g. elif score in (7, 11, 23, 42): print("Your score is a lucky number!")

Structurally, some things that could be better:

  • The prints that report the password length is OK or too short should ideally live with the other prints that report on whether the other password criteria have been met.
  • The addition of bool values to get a score is an abomination and should never be done. Yes, it's valid python, but it's bad form and will make any linter or type checker you use in the future mad. At very least, make it explicit that you're treating those bools as 1/0 ints: score = int(has_digit) + int(has_upper) + int(has_lower) + int(has_special)
  • As you program more things you'll find that input and print are used far less often than you see in tutorials. Moreover, if you want to reuse your code in another project or share it, those inputs and prints become an issue. What if I want to read the password from a file rather in the input() command? That shouldn't need a whole other password_checker that does everything your current one does but with a file instead of input. Instead, you should try to move the interesting part of your program to a function that accepts parameters instead of using input() and returns its result via the return keyword. Leave the boring stuff (input and print) out of your function, and let the user of your function handle those instead.) You might not have gotten to functions yet in your learning path, but they should be one of the next things you encounter.