【问题标题】:Check if two inputs have same index in 2 different lists检查两个输入是否在 2 个不同的列表中具有相同的索引
【发布时间】:2018-05-10 00:29:14
【问题描述】:

大家好,我的计算项目需要帮助,但在谷歌上找不到答案。我正在尝试制作一个基本的登录系统并有 2 个列表,一个带有用户名的列表,一个带有密码的列表:

usernames[username1, username2, username3, etc]

passwords[password1, password2, password3, etc]

我想询问用户输入用户名和密码并检查它们是否在相应的列表中。但是,如果没有人能够使用他们的用户名和其他人的密码登录,我无法弄清楚如何做到这一点。

我当前的代码是:

def Login():

    usernames = [username1, username2, username3]
    passwords = [password1, password2, password3]
    user = input("Please enter your username: ")
    pw = input("Please enter password: ")
    x = 0
    for x in range(len(usernames)):
        if user == usernames[x] and pw == passwords[x]:
            print("Login Successful")
        elif user == usernames[x] and pw !=  passwords[x]:
            print("Password does not match")
            Login()
        else:
            print("User not recognised")
            Login()
        x = x + 1

我希望能够检查他们给我的用户名在列表中的哪个位置,然后在密码列表中查找该位置,如果该密码是他们提供的密码,他们就可以登录。 谢谢!

【问题讨论】:

    标签: python-3.x list


    【解决方案1】:

    您可以使用zip 来遍历您的列表。如果需要查找位置,请使用enumerate

    演示:

    def Login():
        usernames = ['username1', 'username2', 'username3']
        passwords = ['password1','password2', 'password3']
        user = input("Please enter your username: ")
        pw = input("Please enter password: ")
    
        for i, x in enumerate(zip(usernames, passwords)):
            if user == x[0] and pw == x[1]:
                print("Login Successful")
                print("Index Position ", i)
            elif user == usernames[x] and pw !=  passwords[x]:
                print("Password does not match")
                print("Index Position ", i)
                Login()
            else:
                print("User not recognised")
                Login()
    

    【讨论】:

      猜你喜欢
      • 2020-08-20
      • 1970-01-01
      • 2022-12-12
      • 2019-05-16
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多