我的程序是密码强度模拟器,每当我输入密码时,总是输出得分为1。
def upper_case():
points = int(0)
limit = 3
for each in pword:
if each.isupper():
points = points + 1
if points > limit:
points = limit
else:
points = points + 0
return points
pword = raw_input("ENTER: ")
upper_case()
points = 0
points += upper_case()
print points
答案 0 :(得分:2)
您过早地返回points
,因为您将它缩进太远了。删除缩进:
def upper_case():
points = int(0)
limit = 3
for each in pword:
if each.isupper():
points = points + 1
if points > limit:
points = limit
else:
points = points + 0
return points
您可以将其简化为:
def upper_case(pword):
return min((sum(1 for each in pword if each.isupper()), 3))
我将函数更改为参数而不是使用全局。