Hey there,
I recently started learning python and Since I knew a bit of JavaScript, I got a little head start to python that what most beginner learners have when learning python. Since I started learning python, I have been taking challenges and working on my own little project in the mean time. The current challenge I have right now is to make a password validator.
My problem:
I have spent well over 2 hours trying to figure out how to count the number of special chars and numbers in a string (password).
I still haven't perfected the counting but my main problem right now Is getting the function to print out the result
Right, so I have the counter here:
def password_validator():
password = input("Please input your password >>> ")
for element in password:
num_list = ['1','2','3','4','5','6','7','8','9','0',]
for x in num_list:
num_count = 0
if element.count(x):
num_count = 1
if element.count(x):
num_count = 2
num_pass_strength = 1
if num_count == 2:
num_pass_strength = 1
else:
num_pass_strength = 0
elif num_count == 0:
num_pass_strength = 0
spec_chars = ['!', '@', '#', '$', '%', '&', '*',]
for c in spec_chars:
spec_count = 0
if element.count(c):
spec_count = 1
if element.count(c):
spec_count = 2
if spec_count == 2:
spec_pass_strength = 1
else:
spec_pass_strength = 0
elif spec_count == 0:
spec_pass_strength = 0
This basically counts the number of digits and special chars in the password.
This is the how I calculate the password strength:
add_strength = 0
if num_pass_strength == 1:
add_strength += 1
if spec_pass_strength == 1:
add_strength +=1
pass_strength = add_strength
This adds one strength to the add_strength variable, and the pass_strength gets it's data from the add_strength variable aswell.
And this is what I think I am having trouble with:
if pass_strength == 1:
is_weak_pass = True
elif pass_strength == 2:
is_medium_pass = True
elif pass_strength == 3:
is_strong_pass = True
if is_weak_pass == True:
print("Weak")
elif is_medium_pass == True:
print("Medium")
elif is_strong_pass == True:
print("Strong")
This is supposed to check the value of the strength and define a bool - variable for the the desired output.
My main problem right now is though, when I run the script and enter the a password,
it leaves me with an out put like this
Output:
Please input your password >>> &^%&^%
Weak
Weak
Weak
Weak
Weak
Weak
Could somebody please tell me what I have done wrong. It is really bugging me and I would even love to figure out something like this for my other project. Thanks
Full Code:
def password_validator():
password = input("Please input your password >>> ")
for element in password:
num_list = ['1','2','3','4','5','6','7','8','9','0',]
for x in num_list:
num_count = 0
if element.count(x):
num_count = 1
if element.count(x):
num_count = 2
num_pass_strength = 1
if num_count == 2:
num_pass_strength = 1
else:
num_pass_strength = 0
elif num_count == 0:
num_pass_strength = 0
spec_chars = ['!', '@', '#', '$', '%', '&', '*',]
for c in spec_chars:
spec_count = 0
if element.count(c):
spec_count = 1
if element.count(c):
spec_count = 2
if spec_count == 2:
spec_pass_strength = 1
else:
spec_pass_strength = 0
elif spec_count == 0:
spec_pass_strength = 0
add_strength = 0
if num_pass_strength == 1:
add_strength += 1
if spec_pass_strength == 1:
add_strength +=1
pass_strength = add_strength
if pass_strength == 1:
is_weak_pass = True
elif pass_strength == 2:
is_medium_pass = True
elif pass_strength == 3:
is_strong_pass = True
if is_weak_pass == True:
print("Weak")
elif is_medium_pass == True:
print("Medium")
elif is_strong_pass == True:
print("Strong")
password_validator()
question from:
https://stackoverflow.com/questions/65930228/how-to-escape-iterated-loop-python