不知道为啥条件不能运行
password_0 = input("请输入需要检查的密码组合: ")special_characters = ["~","!","@","#","$","%","^",\
"&","*","(",")","_","=","-","/",",",\
".","?","<",">",";",":","[","]",\
"{","}","|"]
# 以下为条件审核
for password in password_0:
if password_0.isalpha():
if password.isalnum() and len(password_0)<=8 :
print("您的密码安全级别评定为: 低")
break
elif password.isdigit() or password.isalpha():
if password in special_characters:
print(password_0)
else:
print("密码输入错误!")
break
print("请按以下方式提升您的安全密码级别:")
print("\t1.密码必须由数字、字母及特殊字符三种组合")
print("\t2.密码只能由字母开头")
print("\t3.密码长度不能低于16位")
课后作业中遇到的情况:
if password in special_characters:
print(password_0)
这个条件为啥不能运行,我试了一晚上,去掉这里的if条件,程序就可以正常运行,但是加上后,就直接调用以下代码块
else:
print("密码输入错误!")
break
但是分开来测试又是可以运行的
special_characters = ["~","!","@","#","$","%","^",\
"&","*","(",")","_","=","-","/",",",\
".","?","<",">",";",":","[","]",\
"{","}","|"]
x="llwertll&llt"
for y in x:
if y in special_characters:
print(y)
求教,这中间是哪个环节出了问题,运行的时候也没有跳出错误代码
elif password.isdigit() or password.isalpha():
if password in special_characters:
print(password_0)
这个条件为啥不能运行?
因为你这个 if 上面的 elif 条件是 判断如果是 password 是数字
或者 字母才执行这里面的代码,所以不可能为你这里的特殊字符
这个代码可以参考参考哈~:
while True:
password_0 = input("请输入需要检查的密码组合: ")
special_characters = ["~", "!", "@", "#", "$", "%", "^", \
"&", "*", "(", ")", "_", "=", "-", "/", ",", \
".", "?", "<", ">", ";", ":", "[", "]", \
"{", "}", "|"]
special,number,word,flag = 0,0,0,0
# 以下为条件审核
if password_0.isalpha():
if len(password_0) <= 8:
print("您的密码安全级别评定为: 低")
flag = 1
elif password_0.isalnum():
print("您的密码安全级别评定为: 低")
flag = 1
else:
for password in password_0:
if password in special_characters:
special = 1
elif password.isdigit():
number = 1
elif password.isalpha():
word = 1
result = special+number+word
if result == 3 and len(password_0) >= 16:
print('恭喜,您的密码安全级别评定为: 高 ,请继续保持!')
break
elif result == 2 and 8< len(password_0) < 16:
print('您的密码安全级别评定为: 中')
flag = 1
else:
print("密码输入错误!")
if flag:
print("请按以下方式提升您的安全密码级别:")
print("\t1.密码必须由数字、字母及特殊字符三种组合")
print("\t2.密码只能由字母开头")
print("\t3.密码长度不能低于16位")
Twilight6 发表于 2020-8-20 23:57
因为你这个 if 上面的 elif 条件是 判断如果是 password 是数字
或者 字母才执行这里面的代码,所 ...
太感谢了,我还是过于刻板的使用这些知识了,不能灵活变通,多谢哈
页:
[1]