python入门小程序:用户登录验证

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/chenhyc/article/details/102724546

题目:自定义函数实现对用户的登录验证,假设所有用户信息存放在一个列表中,列表中,不同用户信息,存放在不同的字典中,实现:用户登录验证,如输入密码错误3次,退出重新输入账号

#多字典元素组成的列表类型
user = [
{'name':'i','password':'i1314','usertype':3},
{'name':'hyc','password':'1314520','usertype':6},
{'name':'love','password':'hyc','usertype':9},
]
user_list = []
for i in range(len(user)):
	print('字典键key是:',user[i]['name'])  #通过user[序列]['name']方式,依次获取字典的value值
	user_list.append(user[i]['name'])    #保存到列表:user_list
print("用户账号有:",user_list)  #将获取到的每一个字典value保存到列表:user_list中,应用场景:用户名称的验证


def logcheck():
	keyerror = 1
	name_input = input("请输入用户名:")
	print(name_input)
	if name_input in user_list:
		dict_index = user_list.index(name_input)     #通过index函数获取“输入用户名”在列表中的序号(位置)
		print("输入用户名在第%d个字典组中" %dict_index)   #获取字典组的次序,主要用于用户名和密码之间的验证
		password_input = input("请输入密码:")
		if password_input == user[dict_index]['password']:   #user[dict_index]['b'],如在第0个字典里,对应的password是:user[0][password]
			print("密码正确,登录成功,欢迎使用python系统")
			print("用户类型:你是第%d个用户" % user[dict_index]['usertype'])
		else:
			while keyerror <=2:
				password_input = input("密码输错%d,请输入密码:" % keyerror)
				if password_input == user[dict_index]['password']:   #user[dict_index]['b'],如在第0个字典里,对应的password是:user[0][password]
					print("密码正确,登录成功,欢迎使用python系统")
					print("用户类型:你是第%d个用户" % user[dict_index]['usertype'])
					break
				keyerror +=1
			else:
				print("密码输入错误3次,已被锁定")
					
	else:
		print("账号不存在,请重新输入,如账号未注册,请点击注册")

while(True):
	logcheck()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/chenhyc/article/details/102724546