如何根据字典键验证用户raw_input:values

时间:2016-05-20 17:47:04

标签: python python-3.x

username = raw_input()
password = raw_input()

user_acct = {'ADMIN':'PASSWORD','READONLY':'PASSWORD','READWRITE':'PASSWORD} 

if username and password != user_acct:
          print('Invalid Login... Please Try Again')
else:

          print('Correct Login')

1 个答案:

答案 0 :(得分:0)

Python将懒惰地评估你的if子句,它包含两个条件!

if username如果不为空则为True

然后无需检查密码。结果,打印出Invalid Login... Please Try Again

你可能想要

  1. 检查用户名是否是词典中的键
  2. 检查密码是否适合此密钥

    if username in user_acct and password == user_acct[username]:
        print('Correct Login')
    else:
        print('Invalid Login... Please Try Again')