Reputation: 47
I have created an if statement in Python, it is a password strength checker. I think it might be the numbers, but I have no clue.
When I type in; aabbcc3 it is meant to be MEDIUM, but it comes up with WEAK. Same as RTH14hl as it comes up with MEDIUM but it's meant to be STRONG.
if u >=6 or l >=6 or n >=6:
print("weak")
elif u > 1 and n > 1:
print("medium")
elif l > 3 and n >= 1:
print("medium")
elif u > 1 and l> 1:
print("medium")
elif u > 3 and l > 2 and nums > 2:
print("strong")
Upvotes: 0
Views: 1043
Reputation: 15934
The problem is that the order in which the statements are set up is producing an effect that you do not want. For example:
elif u > 1 and l> 1:
print("medium")
elif u > 3 and l > 2 and nums > 2:
print("strong")
The last line here will never be executed. Because anything that makes the last conditional true will make the previous conditional be true.
For example if u is 4 and l is 4 then:
elif u > 1 and l> 1:
becomes:
elif 4 > 1 and 4 > 1:
which will evaluate to True
and print "medium"
You can solve this issue by rearranging the order of the statements like so:
elif u > 3 and l > 2 and nums > 2:
print("strong")
elif u > 1 and l> 1:
print("medium")
Essentially you want the hardest things to match be at the top then let non matches fall down to easier to match cases, not the other way around.
Also from the comment made I think it's highly likely that you probably want to generate the u
l
and n
values differently to how you currently are doing it. Python has a nice feature called generator expressions that along with some library function will make your code much more maintainable.
Instead of:
u = p.count("A") + p.count("B") + p.count("C"), ...
l = p.count("a") + p.count("b") + p.count("c"), ...
n = p.count("1") + p.count("2") + p.count("3"), ...
you can do:
u = sum((char.isalpha() for char in p))
l = sum((char.islower() for char in p))
n = sum((char.isdigit() for char in p))
Upvotes: 4