实际上这段代码有错误,但如果我从代码中删除 (!= 1) ,我会得到意想不到的结果。但逻辑是一样的..请解释

Actually there is in error in this code but if I remove (!= 1) from the code I am getting unexpected results. But the logic is same.. Please explain

#问题是判断密码是弱密码还是强密码..

#强密码必须至少有2个数字,2个符号和7个字母

passstr = input()
nc = 0
sc = 0
ln = 0

dnm = "0123456789"
dsc = "!@#$%&*"
ln = len(passstr)
for c in passstr :
  if dnm.find(c) != -1:
     nc += 1
  elif dsc.find(c) != -1:
     sc += 1
if (nc>=2) and (sc>=2) and (ln >= 7):
  print("Strong")
else:
  print("Weak")

修改后的代码:

passstr = input()
nc = 0
sc = 0
ln = 0

dnm = "0123456789"
dsc = "!@#$%&*"
ln = len(passstr)

for c in passstr:
    if c in dmn:
        nc += 1
    elif c in dsc:
        sc += 1

if nc>= 2 and sc >= 2 and ln:
  print("Strong")
else:
  print("Weak")

您还可以制作 dnmdsc 集合,以使成员资格测试(稍微)更快。在这里并不重要,但如果您对几十万个密码执行此操作,可能会有所不同。