Skip to content
Advertisement

How do i list everything in a list [closed]

so i just started learning python, and im making a login interface. this is the interface

username =  input("Username: ")
password = input("Password: ")
correct_username = ["admin", "peter", "lily"]
correct_password = ["admin1", "peter1", "lily1"]
if username == correct_username[0,1,2] and password == correct_password[0,1,2]:
    print("Logged in as: " + username)
else:
    print("Wrong password or username!")

i dont know how to list all correct_username’s and the correct_password’s in “if username == correct_username[0,1,2]” instead of writing them out by hand…

Advertisement

Answer

You can use in to test if a value is in a list.

if username in correct_username and password in correct_password:

However, this won’t test if the username and password are at the same positions — Peter could login with Lily’s password.

Instead, you can get the index of the username in the username list, and compare with the same index in the passwords list.

try:
    username_index = correct_username.index(username)
    if password == correct_password[username_index]:
        print("Logged in as: " + username)
    else:
        Print("Wrong username or password")
except ValueError:
    print("Wrong username or password")

A more idiomatic way to do this would be to collect all the related data together in dictionaries, rather than separate lists for each value.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement