基本用户登录/密码

时间:2018-07-04 23:01:00

标签: python python-3.x

几天前才开始学习python,并且一直在尝试使用我知道的代码来练习从列表中询问用户名和密码的基本代码。我知道用户输入的密码要好得多/更干净/更匹配,但是我现在只是在玩我所知道的东西。

users = ['Jon','Joe', 'Jole']

user_input = input('Username: ')

while user_input != users:
    user_redo = input("I'm sorry but we dont recognize you. Please try another username: ")

这是我的问题所在。如果用户从列表中输入了匹配的用户名,是否有一种简单的方法可以打破循环?

passwords = ['donkey808','Hanna5006']
password = input('Password: ')

我想同样的问题也将适用于密码输入

while password != passwords:
    pw_redo = input(f'Please enter correct password for user {user_input}: ')
else:
    print(f'Access Granted {user_input}')

2 个答案:

答案 0 :(得分:0)

只需while user_input not in users:

not in检查user_input是否确实是不在 users

if user_input not in users:可能也更好,我暂时看不到重点。

答案 1 :(得分:0)

这样写。

users = ["Jon","Joe", "Jole"]
while 0 < 1 :
  user_input = input('Username: ')
  if user_input not in users:
      print("I'm sorry but we dont recognize you. Please try another username: ")
  elif user_input in users:
    break