I'm trying to create a simple login sequence in terminal but I can't figure out how to check if both the username and the password are in the same position in the list
usernames = ['username', 'hello']
passwords = ['password', 'world']
# my code so far:
from getpass import getpass
import time
import logins
username = input('Username: ')
password = input('Password: ')
print('Logging In...')
time.sleep(1)
def check_login(user, pasw):
if user in logins.users and pasw in logins.passes:
print('Valid')
else:
print('Invalid user and password, try again.')
check_login(username, password)
#It works except for the fact that i can input (username, world) or (hello, password) #I'm trying to check that each username and password are in the same order (0,0, 1,1, etc) and #return it as valid. Any help is appreciated :)
CodePudding user response:
You can use zip
:
def check_login(user, pasw):
if (user, pasw) in zip(logins.users, logins.passes):
print('Valid')
else:
print('Invalid user and password, try again.')
If you are to call this method repeatedly, it is better to create a O(1)
lookup structure:
lookup = dict(zip(logins.users, logins.passes))
def check_login(user, pasw):
if lookup.get(user, None) == pasw:
print('Valid')
else:
print('Invalid user and password, try again.')
CodePudding user response:
From what I get about this question, you can just check their indices using index
inbuilt to lists to assert if both the user and password are at the same location.
def check_login(user, pasw):
if user in logins.users and pasw in logins.passes and logins.users.index(user)==logins.passes.index(pasw):
print('Valid')
else:
print('Invalid user and password, try again.')