简体   繁体   中英

Username and Password list or dictionary?

I'm fairly new to python. I've been trying to design a program to match a list of users to their passwords. I've used two seperate lists but am now thinking would a 2d array or a dictionary be better? I'm not sure what technically is the best thing to do as i am self taught/teaching....

here is the code i have got.... any advice welcome.

thanks

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()

This is one way of looking at it, although you can probably find a cleaner way of doing it.

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()

I've been tweaking this to read usernames and passwords from a text file.

logins.txt

containing...

dan,bob,bill

snow,golf,dogs

Here is the code....

def readfile():
    with open('logins.txt') as f:
        users = f.read().splitlines()
        usernames=users[0]
        passwords=users[1]
        list_users=usernames.split(",")
        list_passwords=passwords.split(",")
        print("~~~~~~~~~~~~~~~")
        username=input("Enter your user name: ")
        while username in list_users:
            position=(list_users.index(username))
            attempts=0
            while attempts<3:
                password=input("Enter your password: ")
                if password == list_passwords[position]:
                    print("Log in successfull")
                    print("Hello",username)
                    readfile()
                else:
                    print("Incorrect password")    
                    attempts+=1
                    print("Attempts left: ",3-attempts)
                    if attempts==3:
                        print("Too many attempts!")
                        readfile()
        print("Unknown User")
        readfile()

readfile()

Took a while to figure out why the text file was not being treated as a list but think i've sorted it now...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM