开发者社区> 问答> 正文

用户和密码--使用哪种类型更好,二维数组还是字典?

我试图设计一个程序来匹配用户名和他们的密码。我已经使用了两个独立的列表,但现在我在想2d数组或字典会更好吗? 欢迎任何建议。谢谢

def login():

official_users=['dan','bob','bill']

official_passwords=['snow','golf','dogs']

username=input("Enter your user name: ")

while username in official_users:

    print("You are user number: 

",official_users.index(username)+1)

    position=(official_users.index(username))

    print(position)

    attempts=0

    while attempts<3:

        password=input("Enter your password: ")

        if password == official_passwords[position]:

            print("Log in successfull")

            print("Hello",username)

            login()

        else:

            print("Incorrect password")    

            attempts+=1

            print("Attempts left: ",3-attempts)

            if attempts==3:

                print("Too many attempts!")

                login()

print("Unknown User")

login()

login()

展开
收起
游客6qcs5bpxssri2 2019-10-12 13:06:45 2538 0
1 条回答
写回答
取消 提交回答
  • 希望有帮助

    def login():

    official = {'dan': ['snow', 1],

            'bob': ['golf', 2],
    
            'bill': ['dogs', 3]
    
            }
    

    username=input("Enter your user name: ")

    if username in official:

    print("You are user number: ",official[username][1])
    
    attempts=0
    
    while attempts<3:
    
        password=input("Enter your password: ")
    
        if password == official[username][0]:
    
            print("Log in successfull")
    
            print("Hello",username)
    
            login()
    
        else:
    
            print("Incorrect password")    
    
            attempts+=1
    
            print("Attempts left: ",3-attempts)
    
            if attempts==3:
    
                print("Too many attempts!")
    
                login()
    

    print("Unknown User")

    login()

    2019-10-12 13:08:42
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
用计算和数据去改变整个世界 立即下载
数据+算法定义新世界 立即下载
软件定义的数据智能 立即下载