如何检查两个输入变量是否在两个不同列表中的相同位置并且 return 是否有效?
How do I check if two input variables are in the same position in two different lists and return as valid?
我正在尝试在终端中创建一个简单的登录序列,但我不知道如何检查用户名和密码是否在列表中的相同位置
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)
#除了我可以输入(用户名,世界)或(你好,密码)
#I 试图检查每个用户名和密码的顺序是否相同(0,0,1,1 等)和
#return 有效。感谢您的帮助:)
根据我对这个问题的了解,您可以使用列表中内置的 index
检查它们的索引,以断言用户和密码是否在同一位置。
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.')
您可以使用 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.')
如果要重复调用此方法,最好创建一个O(1)
查找结构:
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.')
我正在尝试在终端中创建一个简单的登录序列,但我不知道如何检查用户名和密码是否在列表中的相同位置
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)
#除了我可以输入(用户名,世界)或(你好,密码) #I 试图检查每个用户名和密码的顺序是否相同(0,0,1,1 等)和 #return 有效。感谢您的帮助:)
根据我对这个问题的了解,您可以使用列表中内置的 index
检查它们的索引,以断言用户和密码是否在同一位置。
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.')
您可以使用 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.')
如果要重复调用此方法,最好创建一个O(1)
查找结构:
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.')