我正在努力使用我的密码评估程序。我如何确保当我插入密码时,我的程序返回密码强度。当我运行这个程序时,它说password_strength存储在.....)是我在程序结束时调用的返回错误吗?
print ("Welcome to this password rater program")
print ("Your password needs to have a minimum of 6 characters with a maximum of 20 characters.")
print ("Your password can contain lowercase and uppercase characters, numbers and special characters")
print("")
password = input("Please insert your password: ")
def check_len(password):
l = len(password)
if 6 < l < 20:
x = check_char(password, l)
else:
x = 0
return x
def check_char(password, l):
for i in range(l):
ascii = ord(password[i])
num = 0
upper = 0
symbols = 0
lower = 0
space = 0
if 96 < ascii < 123:
lower = 15
elif 47 < ascii < 58:
num = 25
elif 64 < ascii < 91:
upper = 25
elif ascii == 32:
space = 30
else:
symbols = 25
total = ((lower + num + upper + space + symbols) * len(password))
return total
def password_strength(total):
if total >= 1000:
print("Your chosen password is Very strong")
elif 700 < total < 999:
print("Your chosen password is strong")
elif 500 < total < 699:
print("your chosen password is medium")
elif total <= 500:
print("Your chosen password is weak")
return total
strength = check_len(password)
print(password_strength(strength))
答案 0 :(得分:3)
首先,你告诉python打印函数,而不是函数的评估。这就是你收到这条消息的原因。
此外,您永远不会调用您编写的任何功能。获得一个工作计划 声明所有定义后如下:
调用check_len定义:
strength = check_len(password)
这个定义虽然没有返回任何值。您可以将其更改为:
def check_len(password):
l = len(password)
if 6 < l < 16:
x = check_char(password, l)
else:
x = 0
return x
让它返回分数/力量。
最后,您应该使用&#39; password_strength&#39;来处理分数。定义:
password_strength(strength)
在这一行中不需要打印,因为打印语句在定义中。如果你想打印最终得分,你也可以进入以下几行:
print(password_strength(strength))
还有一个调试问题:你的check_char定义没有任何参数。 您可以通过将其更改为:
来解决此问题def check_char(password, l):
最终代码: 打印(&#34;欢迎使用此密码评估程序&#34;) 打印(&#34;您的密码至少需要6个字符,最多16个字符。&#34;) 打印(&#34;您的密码可以包含小写和大写字符,数字和特殊字符&#34;) 打印(&#34;&#34;)
password = raw_input("Please insert your password: ")
def check_len(password):
l = len(password)
if 6 < l < 16:
x = check_char(password, l)
else:
x = 0
return x
def check_char(password, l):
for i in range(l):
ascii = ord(password[i])
num = 0
upper = 0
symbols = 0
lower = 0
space = 0
if 96 < ascii < 123:
lower = 15
elif 47 < ascii < 58:
num = 25
elif 64 < ascii < 91:
upper = 25
elif ascii == 32:
space = 30
else:
symbols = 25
total = ((lower + num + upper + space + symbols) * len(password))
return total
def password_strength(total):
if total >= 1000:
print("Your chosen password is Very strong")
elif 700 < total < 999:
print("Your chosen password is strong")
elif 500 < total < 699:
print("your chosen password is medium")
elif total <= 500:
print("Your chosen password is weak")
return total
strength = check_len(password)
print(password_strength(strength)) `
答案 1 :(得分:1)
你永远不会调用你的python函数。
def potatis():
print("Hello yu!")
定义了一个函数,但是你还需要为实际运行的代码调用函数potatis()
。