小白成长记-----python实现注册的小程序


# 3、写一个注册的程序,输入username,密码,
# 密码确认,输入的账号和密码不能为空,两次输入密码必须一致,
# 用户名不能重复,错误次数四次 。注册成功提示成功
# 把注册账号密码信息的写到文件里头


user=[] #定义一个列表,存username
f= open('username.txt','a+') #打开文件
f.seek(0) #指向文件的最开头的位置
for line in f: #将f里面的全部遍历一遍,
username=line.split(',')[0] #取逗号前面的一串字符
user.append(username) #将取出来的username加入user列表

i=0
while (i<4):
username=input('用户名:').strip()
passwd=input('密码:').strip()
c_passwd=input('确认密码:').strip()
if username=='' or passwd=='' or c_passwd=='':
i+=1
print('用户名或密码不能为空:')
continue
else:
if username in user:
print('已经被注册')
continue
elif passwd==c_passwd:
print('注册成功:%s' %username)
f.write(username+','+passwd+' ')
f.close()
break
else:
print('两次密码不一致')
i+=1
else:
print(' 错误次数超过4次 Outing......')
f.close()


小白本次遇到的是问题在不知如何将变量username存入文件中
此处参考了小伙伴的博客:
http://www.cnblogs.com/snorth/p/6932953.html



原文地址:https://www.cnblogs.com/zunchang/p/7577124.html