注册程序怎么写??【python】
思路:如下图所示
代码实现:
name_list = {}
f = open('registered.txt', "a+", encoding='utf-8') #一定要使用a+,不能使用r、r+ 如果文件不存在会报错的
f.seek(0)
list=f.readlines()
# if len(list)!=0:
# del list[0]
for read in list:
b = read.strip()
a = b.split(",")
name_list[a[0]]=a[1]
f.close()
for i in range(3):
name = input("请输入您要注册的名字:").strip()
if len(name)==0:
print("输入用户为空")
continue
elif name in name_list:
print("%s已注册" %name)
break
else:
password = input("请输入密码:") .strip()
passwordcp = input("请输入确认密码:").strip()
if len(password and passwordcp)==0:
print("密码不能输入为空")
continue
elif password == passwordcp :
if len(password) > 13 or len(password) < 6:
print("密码长度要求为 '6-9' 位数之间")
continue
else:
f = open('registered.txt', "a+", encoding='utf-8')
a=name,password
name_password = ','.join(a)
f.write( name_password+"\n")
f.close()
print("您已注册成功")
break
else:
print("您输入的密码不正确")
i=i+1
else:
print("您输入已超过3次")