我似乎无法使用此代码添加函数中执行的所有点,然后在最后输出它。每次输入另一个密码时,它返回值1。任何帮助都会很棒!谢谢!!
def length_pw():
points = int(0)
length = len(pword)
if length < 6:
print "%s is REJECTED"%pword
if length <=8:
points = points + 1
return points
else:
points = points + 0
print "%s is ACCEPTED"%pword
def upper_case():
points = int(0)
limit = 3
for each in pword:
if each.isupper():
points = points + 1
return points
else:
points = points + 0
def num_digits():
points = int(0)
limit= 3
for each in pword:
if each.isdigit():
points = points + 1
return points
else:
points = points + 0
def non_alpha_numeric():
points = int(0)
limit= 3
for each in pword:
if each.isalnum():
points = points + 1
return points
else:
points = points + 0
while True:
pword = raw_input("Enter Password: ")
points = 0
points = length_pw()
points = upper_case()
points = num_digits()
points = non_alpha_numeric()
print points
答案 0 :(得分:1)
您依次将每个函数的返回值分配给points
值。您要做的是分配其当前值和函数返回值的总和。这意味着
points = 0
points = points + length_pw()
points = points + upper_case()
etc...
Python(以及大多数其他语言)为此操作提供了一些简化的语法:
points = 0
points += length_pw()
points += upper_case()
etc...
答案 1 :(得分:0)
您需要在每次函数调用后添加点。 E.g。
points += length_pw()
points += upper_case()
points += num_digits()
points += non_alpha_numeric()
答案 2 :(得分:0)
您遇到的根本问题是因为您的功能没有正确return
分值。第一个并不总是返回任何内容(与返回None
的内容相同。其他内容将在处理pword
的第一个字母后停止。
这是尝试解决这些问题并清理或简化代码中的其他几项内容:
def length_pw(pword):
points = 0
length = len(pword)
if length < 6:
print "%s is REJECTED" % pword
if length <= 8: # this will always be True...
points += 1
else:
print "%s is ACCEPTED" % pword
return points
def eval_pword(pword, method_name):
return sum(getattr(char, method_name)() for char in pword)
def upper_case(pword):
return eval_pword(pword, 'isupper')
def num_digits(pword):
return eval_pword(pword, 'isdigit')
def non_alpha_numeric(pword):
return len(pword) - eval_pword(pword, 'isalnum') # total - alpha-numeric
while True:
pword = raw_input("Enter Password: ")
points = (length_pw(pword) + upper_case(pword) +
num_digits(pword) + non_alpha_numeric(pword))
print points