Home » Python » python – Need help defining a list without changing the value it already has-Exceptionshub

python – Need help defining a list without changing the value it already has-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

Ok, I’m pretty new to python and I was trying to see if I could make a simple login/register system that just uses usernames and passwords. I added this at the start of the code so I didn’t have to manually restart it every time I want to test something:

loop = True
while loop == True:

then, I made two lists called ‘accountUsernames’ and ‘accountPasswords’ with just a couple of random usernames and passwords. The idea was that every account would have its password and username index be the same, and the code would check if the username and password the user entered while logging in had the same index. When I started writing the code of the register system, I realized I was kind of stuck. I was using the append feature to add the username and password the user had entered into the previously mentioned lists, but when it did so, the code would loop back to the start because it was over, meaning the lists would also be changed to their previous state. I was wondering if there was a way I could define those lists at the start without giving them any values, or changing the values it already has from the previous loop. Here’s the full code:

loop = True
while loop == True:
    accountUsernames = ['a', '1']
    accountPasswords = ['b', '2']
    lr = input('Would you like to login or register?\n')
    if lr.lower() == 'login':
        loginUsername = input('Please enter your username.\n')
        loginPassword = input('Please enter your password.\n')
        if loginUsername in accountUsernames:
            loginIndex = accountUsernames.index(loginUsername)
            if accountPasswords[loginIndex] == loginPassword:
                print('You have successfully logged in!')
            else:
                print('Invalid username or password. Please try again.')
        else:
            print('Invalid username or password. Please try again.')
    elif lr.lower() == 'register':
        registerUsername = str(input('Please enter a username.\n'))
        registerPassword = str(input('Please enter a password.\n'))
        registerPasswordConfirmation = str(input('Please confirm your password.\n'))
        if registerUsername in accountUsernames:
            print('That username is already taken. Please try again.')
        elif registerPassword != registerPasswordConfirmation:
            print('These passwords do not match. Please try again.')
        else:
            accountUsernames.append(registerUsername)
            accountPasswords.append(registerPassword)
            print('You have successfully registered! You can now log in.')

I know it probably has a lot of glaring issues but as I said, I’m pretty new to python. Also, sorry if I over/under-explained the issue. I would really appreciate your help.

How to&Answers:

I don’t really understand the question, but why don’t you define the accountUsernames and accountPasswords before the loop starts, like this:

accountUsernames=[]
accountPasswords=[]
while loop:
   #insert loop code here

Another suggestion that you should implement is instead of having two lists, to have a dictionary.

userData={}
def addUser(username, password):
    global userData
    userData[username]=password

def checkUser(username,password):
    global userData
    if username in userData:
        if password==userData[username]:
            return True
    return False

loop=True
while loop:
    #insert code

Answer:

Just place the accountUsernames and accountPasswords lists outside the loop like this:

accountUsernames = ['a', '1']
accountPasswords = ['b', '2']
loop = True
while loop == True:
    lr = input('Would you like to login or register?\n')
    if lr.lower() == 'login':
        loginUsername = input('Please enter your username.\n')
        loginPassword = input('Please enter your password.\n')
        if loginUsername in accountUsernames:
            loginIndex = accountUsernames.index(loginUsername)
            if accountPasswords[loginIndex] == loginPassword:
                print('You have successfully logged in!')
            else:
                print('Invalid username or password. Please try again.')
        else:
            print('Invalid username or password. Please try again.')
    elif lr.lower() == 'register':
        registerUsername = str(input('Please enter a username.\n'))
        registerPassword = str(input('Please enter a password.\n'))
        registerPasswordConfirmation = str(input('Please confirm your password.\n'))
        if registerUsername in accountUsernames:
            print('That username is already taken. Please try again.')
        elif registerPassword != registerPasswordConfirmation:
            print('These passwords do not match. Please try again.')
        else:
            accountUsernames.append(registerUsername)
            accountPasswords.append(registerPassword)
            print('You have successfully registered! You can now log in.')