zancun

该博客展示了一段Python代码,实现了用户登录注册功能。代码定义了UserLogin类,包含初始化、登录和保存用户信息的方法。登录时会对用户名、密码进行多项验证,如是否为空、用户名是否已存在、两次密码是否一致、长度是否符合要求等,验证通过后保存用户信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 class UserLogin:
 2     def __init__(self):
 3         self.file_name = "user_info"
 4         self.user_info = dict(
 5             [(line.strip().split(",")[0].lower(), line.strip().split(",")[1]) for line in open(self.file_name, "rb")
 6              if line])
 7 
 8     def login(self):
 9         for i in range(3):
10             user_name = raw_input('username:')
11             pwd = raw_input('passwd:')
12             cpwd = raw_input('cpasswd:')
13 
14             if not user_name or not pwd or not cpwd:
15                 print "账号、密码和确认密码都不能为空!"
16                 continue
17             if str(user_name).lower() in self.user_info:
18                 print "用户名已存在!"
19                 continue
20             if pwd != cpwd:
21                 print "两次密码输入不一致!"
22                 continue
23             if not (len(user_name) > 6 and len(user_name) < 12) or not (len(pwd) > 6 and len(pwd) < 12):
24                 print "账号和密码长度要大于等于6,小于等于12"
25                 continue
26 
27             self.user_info[user_name] = pwd
28             break
29         self.save()
30 
31     def save(self):
32         fw = open(self.file_name, "wb")
33         for user_name, pwd in self.user_info.items():
34             fw.write("{user_name},{pwd}\n".format(user_name=user_name.lower(), pwd=pwd))
35         fw.close()
36 
37 
38 userLogin = UserLogin()
39 userLogin.login()

 

转载于:https://www.cnblogs.com/xiaokuangnvhai/p/10934165.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值