关于14讲的课后题,纯符号 bug
symblos = r'''`!@#$%^&*()_+-=/*{}[]\|'";:/?,.<>'''chars = 'abcdeefghijklnmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
nums = '0123456789'
pw = input('请输入需要检测的密码组合:')
lenght = len(pw)
while (pw.isspace() or lenght == 0):
pw = input('需有密码不能为空,重新输入:')
lenght = len(pw)
for a in pw:
if a in symblos and (a not in nums or a not in nums):
pw = input('需有密码不能纯符号,重新输入:')
lenght = len(pw)
if lenght <= 8:
flag_len = 1
elif 8 < lenght < 16:
flag_len = 2
elif lenght >= 16:
flag_len = 3
flag_con = 0
for each in pw:
if each in symbols:
flag_con += 1
break
for each in pw:
if each in chars:
flag_con += 1
break
for each in pw:
if each in nums:
flag_con += 1
break
while True :
print('您的密码安全级别为:', end='')
if flag_len == 1 or flag_con == 1:
print('低')
elif flag_len == 3 and flag_com ==3 and (pw in chars):
print('高')
else:
print('中')
我加了一个循环来判断是否是纯符号,测试是ok的,看看大神们有木有更好的解答
本帖最后由 Twilight6 于 2020-7-29 16:54 编辑
其他先不说哈,你代码中的:
for each in pw:
if each in symbols:
以及
elif flag_len == 3 and flag_com ==3 and (pw in chars):
都错了,正确的是 symblos和 flag_con
帮你改了下代码中的错误,最后你的 while 循环也是个死循环,改完的代码只有输入为高级密码才会退出程序:
symblos = r'''`!@#$%^&*()_+-=/*{}[]\|'";:/?,.<>'''
chars = 'abcdeefghijklnmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
nums = '0123456789'
pw = input('请输入需要检测的密码组合:')
lenght = len(pw)
while True:
while (pw.isspace() or lenght == 0):
pw = input('需有密码不能为空,重新输入:')
lenght = len(pw)
while True:
for a in pw:
if a not in symblos:
break
else:
pw = input('需有密码不能纯符号,重新输入:')
lenght = len(pw)
continue
break
if lenght <= 8:
flag_len = 1
elif 8 < lenght < 16:
flag_len = 2
elif lenght >= 16:
flag_len = 3
flag_con = 0
for each in pw:
if each in symblos:
flag_con += 1
break
for each in pw:
if each in chars:
flag_con += 1
break
for each in pw:
if each in nums:
flag_con += 1
break
print('您的密码安全级别为:', end='')
if flag_len == 1 or flag_con == 1:
print('低')
elif flag_len == 3 and flag_con ==3 and (pw in chars):
print('恭喜你,你的密码强度为:高!')
break
else:
print('中')
pw = input('请重新输入需要检测的密码组合:')
lenght = len(pw) 没有什么办法比正则更简单 方法是不对的,经过测试会报错 Twilight6 发表于 2020-7-29 17:06
帮你改了下代码中的错误,最后你的 while 循环也是个死循环,改完的代码只有输入为高级密码才会退出程序 ...
程序运行后还是有bug的,按照这个程序,输入:@@@@@@@@@@@@@,会显示‘需要有密码不能纯符号,重新输入:’,到这没问题,可是接下来不输入或输入空格,就会显示‘您的密码安全级别为:低’。我在第13——22行进行了改进:
while True:
while (pw.isspace() or lenght == 0):
pw = input('需有密码不能为空,重新输入:')
for a in pw:
if a not in symblos:
break
else:
pw = input('需有密码不能纯符号,重新输入:')
lenght = len(pw)
continue
抱歉,不怎么会回复,只能将就着看了。。。。
页:
[1]