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()