[英]Why does my for loop not check every character?
So I'm a bit of a beginner at python and I cant for the life of me figure out why it wont check and add on for every letter 所以我是python的新手,我一辈子都无法弄清楚为什么它不会检查并添加每个字母
def howstrong (password):
points = len(password)
charactersallowed = ["!", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+"]
for ch in password:
if ch.isupper():
points= points+5
elif ch.islower():
points = points+5
elif ch.isdigit():
points = points+5
elif ch in charactersallowed:
points= points+5
else:
points = points+0
return points
If I input a password of Password! 如果我输入密码为密码! into my code it tells me my points are 14 but for this password it should be 24?
在我的代码中,它告诉我我的积分是14,但是对于此密码,它应该是24? Below I will add the rest of my code but I doubt that its in that part and I believe that there's an error in my for loop somewhere.
在下面,我将添加其余的代码,但我对此部分表示怀疑,并且我认为我的for循环中某处存在错误。
def checkingmain():
while 1:
p = input("\nEnter password: ")
if len(p) < 8 or len(p) > 24:
print("Password must be 6 to 12 characters.")
elif input("Re-enter password: ") != p:
print("Passwords did not match. Please try again.")
else:
score= howstrong(p)
if not score:
print("Invalid character detected. Please try again.")
else:
if score <= 0:
print("your password is weak",score)
elif score>0 and score<20:
print("your password is medium",score)
else:
print("your password is strong",score)
break
I'd appreciate if someone would get back to me with an understandable solution for a somewhat python beginner. 如果有人能为某个python初学者提供一个可理解的解决方案,我将不胜感激。
It only ever checks the first character, because you return inside the loop. 它只检查第一个字符,因为您在循环内返回。 Move your return statement back one indent, so it is not inside the for loop.
将return语句移回一个缩进,因此它不在for循环内。
Since you have your return statement inside the loop, it only runs the loop one time before returning from the function. 由于您在循环内有return语句,因此它仅在从函数返回之前运行循环一次。 If you move your return statement back one tab it should work
如果将return语句移回一个选项卡,它应该可以工作
The return statement is inside the for loop , so when your program reaches the end of the for loop for the first time, it simply returns from the function, so your for loop is terminated. return语句位于for循环内,因此,当您的程序第一次到达for循环的末尾时,它只是从函数中返回,因此for循环终止。 A little change in your code like shown below will help you.
如下所示,对代码进行一些小的更改将对您有所帮助。
for ch in password:
if ch.isupper():
points= points+5
elif ch.islower():
points = points+5
elif ch.isdigit():
points = points+5
elif ch in charactersallowed:
points= points+5
else:
points = points+0
return points
Hope it helps ! 希望能帮助到你 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.