-4

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...

Gergő
  • 3
  • 1
  • Does this answer your question? [Check if something is (not) in a list in Python](https://stackoverflow.com/questions/10406130/check-if-something-is-not-in-a-list-in-python) – LLSv2.0 Mar 11 '20 at 18:35

2 Answers2

3

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

if username in correct_username:

LLSv2.0
  • 501
  • 6
  • 20