验证登录名和密码

时间:2016-11-17 01:04:34

标签: python

def IsValidLogin(login_name):
  lognum = 0
  login = 0

  if len(login_name) >= 8:
    lognum == True
  elif len(login_name) < 8:
    lognum == False


  for chr in login_name:
    if chr.isalnum() == True:
        login == True
    else:
        login == False

  if (login == True) and (lognum == True):
    IsValidLogin  == True
  else:
    IsValidLogin == False

  if IsValidLogin == True:
    print('Login Accepted')
  else:
    print('Login Failed')

  return lognum, login


login_name = input('Please enter your Login: ')
lognum, login = IsValidLogin(login_name)

def IsValidPassword(password):
  pass_char = ['!','@','#','$','%','^','&','*','(',')','_','+']

  pass_len = 0
  pass_num = 0
  spec_char = 0

  for chr in password:
    if len(password) >= 8:
        pass_len == True
    elif len(password) < 8:
        pass_len == False

  for chr in password:
    if chr.isdigit() == True:
        pass_num == True
    elif chr.isdigit() == False:
        pass_num == False

  for chr in password:
    if password == pass_char:
        spec_char == True
    elif password != pass_char:
        spec_char == False

  if (pass_len == True) and (spec_char == True):
    IsValidPassword == True
  else:
    IsValidPassword == False

  if IsValidPassword == True:
    print('Password Accepted')
  else:
    print('Password Failed')

  return pass_len, pass_num, spec_char 

password = input('Please enter your password: ')
pass_len, pass_num, spec_char = IsValidPassword(password)

登录名必须至少8个字符,并且只能包含字母和数字。

密码长度必须至少为8个字符,至少包含一个数字和一个特殊字符。

程序运行,但都返回false。

想知道我做错了什么。请记住,我是Python的新手。

1 个答案:

答案 0 :(得分:0)

行:

  if len(login_name) >= 8:
    lognum == True
  elif len(login_name) < 8:
    lognum == False

似乎错了。您似乎正在尝试分配给lognum,而是将lognumTrueFalse进行比较。 =是作业,==是比较。类似于:

if chr.isalnum() == True:
    login == True
else:
    login == False

  if (login == True) and (lognum == True):
    IsValidLogin  == True
  else:
    IsValidLogin == False

以及该职能以外的其他几个地方。

只需将相应的==更改为=即可。

即使进行了此更改,密码部分也存在逻辑问题:这将始终返回false,因为它只检查最后一个字符,该字符不能是数字和特殊字符。

  for chr in password:
    if chr.isdigit() == True:
        pass_num = True
    elif chr.isdigit() == False:
        pass_num = False #On the last pass through, this overwrites the previous value

  for chr in password:
    if password == pass_char:
        spec_char = True
    elif password != pass_char:
        spec_char = False #On the last pass through, this overwrites the previous value

elif password != pass_char:更改为elif not spec_char:,将elif chr.is_digit():更改为elif not pass_num:,以防止被覆盖。