Python 如何检查语句是否匹配(Zip函数)
所以我现在正在学校里做一个登录功能的小项目。我尝试将用户名和密码存储在列表中,并使用[0:1]过程进行检查。是否有一种方法可以检查用户输入是否与选定部分中的压缩列表匹配? 对不起,如果这是超级简单,我是相当新的编码 代码:Python 如何检查语句是否匹配(Zip函数),python,authentication,Python,Authentication,所以我现在正在学校里做一个登录功能的小项目。我尝试将用户名和密码存储在列表中,并使用[0:1]过程进行检查。是否有一种方法可以检查用户输入是否与选定部分中的压缩列表匹配? 对不起,如果这是超级简单,我是相当新的编码 代码: Profile = ['jc1', 'jc2', 'jc3', 'jc4', 'jc5'] Passwords = ['123','213','312','321','231'] Prof = zip(Profile[0:1],Passwords[0:1]) A = li
Profile = ['jc1', 'jc2', 'jc3', 'jc4', 'jc5']
Passwords = ['123','213','312','321','231']
Prof = zip(Profile[0:1],Passwords[0:1])
A = list(Prof)
JK = input('Please enter your username: ')
DK = input('Please enter your password: ')
Entry = zip(JK,DK)
B = list(Entry)
if B == A:
print('Welcome User: jc1')
print(A)
我想您可以使用这两个列表的索引值进行配置文件到密码的匹配
Profile = ['jc1', 'jc2', 'jc3', 'jc4', 'jc5']
Passwords = ['123','213','312','321','231']
JK = raw_input('Please enter your username: ')
if JK in Profiles:
DK = raw_input('Please enter your password: ')
if DK in Passwords and Profiles.index(JK) == Password.index(DK):
print "Welcome user %s"%(JK)
else:
print "Incorrect Password"
else:
print "User %s not in Profile list"%(JK)
现在,您可以使用列表,但如果您了解
字典
,并尝试通过字典实现相同的功能,则会更好。这就是您可以在字典
中存储配置文件名
和密码
组合,并使用用户输入进行检查的方式
profile_password_mapping = ['jc1':'123', 'jc2':'213', 'jc3':'312', 'jc4':'321', 'jc5':'231']
profileName = input('Please enter your username: ')
profilePassword = input('Please enter your password: ')
if profileName in profile_password_mapping.keys() and profile_password_mapping[profileName] == profilePassword:
print('Welcome User: {0}'.format(profileName))
else:
print("User does not exist or the password is wrong.")
您所做的一切可以表示为
JK==Profile[0]和DK==Passwords[0]
。您只是在检查第一个条目…您可能想检查输入的值是否与配置文件/密码列表中的任何组合匹配…!?为什么不使用字典
来存储档案名称密码
组合而不是列表?我只检查第一个条目,因为我不确定如何获取zip
来开始工作。我将尝试使用字典
,看看是否可以使用。@PrashantKumar for字典
如何使密码与每个用户关联?再次为缺乏知识而抱歉…[…:…,…]
…!?