用户注册
一、需求
1、新建一个txt,写入部分用户名、密码
2、读取txt,判断用户是否存在,存在则提示存在
3、新注册的用户,写入txt
二、实现代码
with open('users.txt', 'r', encoding='utf-8') as f: users = [] for line in f.readlines(): line = line.strip('\n') # 去掉换行符\n b = line.split(',') # 将每一行以逗号为分隔符转换成列表 users.append(b) dic = dict(users) #将list转为字典 print(dic) while True: username = input('username:').strip() pwd = input('password:').strip() cpwd = input('password:').strip() if username == '' and pwd =='' and cpwd =='': print('用户名或密码不能为空') elif username in dic: print('用户已存在') elif pwd !=cpwd: print('两次密码输入不一致') else: print('欢迎{}注册'.format(username)) with open('users.txt', 'a', encoding='utf-8') as f1: f1.write(username+','+pwd +'\n') f1.close()

浙公网安备 33010602011771号