Python 学习之登录注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def register(userinfo, name, passwd):
with open(userinfo, mode="r+", encoding="utf-8") as f1:
for line in f1:
# print(line.split("")[0])
if line.split("")[0] == name:
return 0,"{0}用户已注册,请更换其他用户名".format(name)
f1.write(name+""+passwd+"\n")
return 1,"注册成功,用户名是:{0},密码是:{1}".format(name,passwd)


def login(userinfo, name, passwd):
with open(userinfo, mode="r", encoding="utf-8") as f1:
for line in f1:
# print(line.split("")[0])
# print(line.split("")[1].strip())
if name == line.split("")[0]:
if passwd == line.split("")[1].strip():
return 1,"用户名和密码正确,登录成功",0
else:
return 0,"{0}用户名密码错误,请检查".format(name),"loginerror"
return 0, "{0}用户名还没注册,请先注册".format(name),0

# flag = 1
# while flag:
# name = input("请输入用户名:")
# passwd = input("请输入密码:")
# msg = register("userinfo", name, passwd)
# flag = msg[0]
# print(msg[1])

name = input("请输入用户名:")
count = 1
while count <=3:
passwd = input("请输入密码:")
msg = login("userinfo", name, passwd)
if msg[0] == "1":
print(msg[1])
break
else:
if msg[2] == "loginerror":
print(msg[1])
print("已尝试{0}次,还剩余{1}次".format(count, 3 - count))
count += 1
else:
print(msg[1])
break