第14讲:设置密码安全
我想要添加机会制度,但是只能多进行1次,而且第二次不会进行等级判断,这是什么原因,然后就是我感觉我写的好复杂,有没有大佬帮忙改下??代码是这样的:
symbols = r'''`!@#$%^&*()_+-=/*{}[]\|'";:/?,.<>'''
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
nums = '0123456789'
times = 10
passwd = input('请输入需要检查的密码组合:')
# 判断长度
length = len(passwd)
while (passwd.isspace() or length == 0):
passwd = input("您输入的密码为空(或空格),请重新输入:")
length = len(passwd)
if length <= 8:
flag_len = 1
elif 8 < length < 16:
flag_len = 2
else:
flag_len = 3
flag_con = 0
# 判断是否包含特殊字符
for each in passwd:
if each in symbols:
flag_con += 1
break
# 判断是否包含字母
for each in passwd:
if each in chars:
flag_con += 1
break
# 判断是否包含数字
for each in passwd:
if each in nums:
flag_con += 1
break
# 打印结果
while 1:
times = times - 1# 用户每输入一次,可用机会就-1
print("您的密码安全级别评定为:", end='')
if flag_len == 1 or flag_con == 1:
print("低")
if times > 0:
question = input("是否重新输入:")
if (question == "是"):
passwd = input("请重新输入:")
length = len(passwd)
elif (question == "否"):
print("设置完成!")
elif flag_len == 3 and flag_con == 3 and (passwd in chars):
print("高")
print("请继续保持")
break
else:
print("中")
if times > 0:
question = input("是否重新输入:")
if (question == "是"):
passwd = input("请重新输入:")
length = len(passwd)
elif (question == "否"):
print("设置完成!")
print("最高安全级别设置方法:\n\
\t1. 密码必须由数字、字母及特殊字符三种组合\n\
\t2. 密码只能由字母开头\n\
\t3. 密码长度不能低于16位")
break def check_number_exist(password_str):
"""
判断字符串中是否含有数字
"""
has_number = False
for c in password_str:
if c.isnumeric():
has_number = True
break
return has_number
def check_letter_exist(password_str):
"""
判断字符串中是否含有字母
"""
has_letter = False
for c in password_str:
if c.isalpha():
has_letter = True
break
return has_letter
def main():
"""
主函数
"""
try_times = 5
while try_times > 0:
password = input('请输入密码:')
# 密码强度
strength_level = 0
# 规则1:密码长度大于9
if len(password) >= 8:
strength_level += 1
else:
print('密码长度要求至少八位!')
# 规则2:包含数字
if check_number_exist(password):
strength_level += 1
else:
print('密码要求包含数字!')
# 规则3:包含字母
if check_letter_exist(password):
strength_level += 1
else:
print('密码要求包含字母!')
if strength_level == 3:
print('恭喜!密码强度合格!')
break
else:
print('密码强度不合格!')
try_times -= 1
print('_________________')
print()
if try_times <= 0:
print('尝试次数过多,密码设置失败!')
if __name__ == '__main__':
main() yayc_zcyd 发表于 2021-2-25 14:07
大佬,你咋给全改了呢,我还是个小萌新,看不太懂你这个{:10_291:}{:10_282:} 孤妓与酒 发表于 2021-2-25 14:39
大佬,你咋给全改了呢,我还是个小萌新,看不太懂你这个
...这个是一个while循环啊{:10_249:} 这就是加了几个函数 yayc_zcyd 发表于 2021-2-25 14:43
...这个是一个while循环啊
{:10_247:},一个循环就这么长,我有点怀疑人生了 symbols = r'''`!@#$%^&*()_+-=/*{}[]\|'";:/?,.<>'''
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
nums = '0123456789'
times = 3
tip = "最高安全级别设置方法:\n\
\t1. 密码必须由数字、字母及特殊字符三种组合\n\
\t2. 密码只能由字母开头\n\
\t3. 密码长度不能低于16位"
while times:
times = times - 1# 用户每输入一次,可用机会就-1
passwd = input('请输入需要检查的密码组合:')
# 判断长度
length = len(passwd)
while (passwd.isspace() or length == 0):
passwd = input("您输入的密码为空(或空格),请重新输入:")
length = len(passwd)
if length <= 8:
flag_len = 1
elif 8 < length < 16:
flag_len = 2
else:
flag_len = 3
flag_con = 0
# 判断是否包含特殊字符
for each in passwd:
if each in symbols:
flag_con += 1
# 判断是否包含字母
for each in passwd:
if each in chars:
flag_con += 1
# 判断是否包含数字
for each in passwd:
if each in nums:
flag_con += 1
# 打印结果
print("您的密码安全级别评定为:", end='')
if flag_len == 1 or flag_con == 1:
print("低")
print(tip)
if times > 0:
question = input("是否重新输入:")
if (question == "是"):
continue
elif (question == "否"):
print("设置完成!")
elif flag_len == 3 and flag_con == 3 and (passwd in chars):
print("高")
print("请继续保持")
break
else:
print("中")
print(tip)
if times > 0:
question = input("是否重新输入:")
if (question == "是"):
continue
elif (question == "否"):
print("设置完成!")
逃兵 发表于 2021-2-25 14:46
建议在最前面加一个:
print("一共有{}次机会。".format(times)) 都是大佬,谢谢你们 逃兵 发表于 2021-2-25 14:46
大佬,如果我想低中高都判定是否重新输入要怎么改合适啊,毕竟人家安全系数高但是不一定喜欢嘛,是吧,再次求助{:10_298:} 孤妓与酒 发表于 2021-2-25 15:20
大佬,如果我想低中高都判定是否重新输入要怎么改合适啊,毕竟人家安全系数高但是不一定喜欢嘛,是吧,再 ...
我改一改 symbols = r'''`!@#$%^&*()_+-=/*{}[]\|'";:/?,.<>'''
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
nums = '0123456789'
times = 3
tip = "最高安全级别设置方法:\n\
\t1. 密码必须由数字、字母及特殊字符三种组合\n\
\t2. 密码只能由字母开头\n\
\t3. 密码长度不能低于16位"
print("一共有{}次机会。".format(times))
while times:
times = times - 1# 用户每输入一次,可用机会就-1
passwd = input('请输入需要检查的密码组合:')
# 判断长度
length = len(passwd)
while (passwd.isspace() or length == 0):
passwd = input("您输入的密码为空(或空格),请重新输入:")
length = len(passwd)
if length <= 8:
flag_len = 1
elif 8 < length < 16:
flag_len = 2
else:
flag_len = 3
flag_con = 0 #特殊字符数
flag_con1 = 0 #字母数
flag_con2 = 0 #数字
# 判断是否包含特殊字符
for each in passwd:
if each in symbols:
flag_con += 1
# 判断是否包含字母
for each in passwd:
if each in chars:
flag_con1 += 1
# 判断是否包含数字
for each in passwd:
if each in nums:
flag_con2 += 1
# 打印结果
print("您的密码安全级别评定为:", end='')
if flag_len == 1 or flag_con == 0 or flag_con1 == 0 or flag_con2 == 0:
print("低")
print(tip)
if times > 0:
question = input("是否重新输入:")
if (question == "是"):
print("还有{}次机会。".format(times))
continue
elif (question == "否"):
print("设置完成!")
break
elif flag_len == 3 and flag_con and flag_con1 and flag_con2 and (passwd in chars):
print("高")
if times > 0:
question = input("是否重新输入:")
if (question == "是"):
print("还有{}次机会。".format(times))
continue
elif (question == "否"):
print("设置完成!")
break
else:
print("中")
print(tip)
if times > 0:
question = input("是否重新输入:")
if (question == "是"):
print("还有{}次机会。".format(times))
continue
elif (question == "否"):
print("设置完成!")
break
逃兵 发表于 2021-2-25 16:06
牛批,谢谢大佬
页:
[1]