实现用户注册功能---文件保存信息,如果用户名存在就死循环继续

#   一、实现用户注册功能
# 思路:
# 用户输入用户名、密码
# 将用户输入的内容按照固定的格式,比如:egon:123,存入文件
# 可以往一个文件中重复注册新的用户名和密码

with open(r'Text.txt', mode='wt', encoding='utf-8')as f1, \
open(r'Text.txt', mode='rt', encoding='utf-8')as f2,\
open(r'Text.txt', mode='at', encoding='utf-8')as f3:
user_name = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
f1.write(user_name + ':' + password+'\n') #一开始注册了一个用户名密码存入
f1.close()
a = f2.readline().split(':')
print(a)
f2.close()
while True:
user_name11 = input('请输入用户名:').strip()
if a[0] == user_name11 :
print('用户名存在!')
continue
else:
print('用户名未被注册。')
password11 = input('请输入密码:').strip()
f3.write(user_name11 + ':' + password11)
break
f3.close()

猜你喜欢

转载自www.cnblogs.com/wangcheng9418/p/9134472.html