# 注册
def register():
count = 0
while count < 3:
Username = input("输入用户名:")
password = input("输入密码:")
re_password = input("再次输入密码:")
if not re_password == password:
print('两次输入不一致')
count+=1
continue
with open('userinfo.txt', 'a', encoding='utf8')as fa:
fa.write(f'{Username}:{password}
')
fa.flush()
print('注册成功')
break
# register()
# 登陆
def login():
Username_inp = input("输入用户名:")
password_inp = input("输入密码:")
with open('userinfo.txt','r',encoding='utf-8')as fr:
for userinfo in fr:
username,password=userinfo.split(':')
if username.strip()==Username_inp and password.strip()==password_inp:
print('登陆成功')
break
else:
print('登陆失败')
register()
login()
这里用了明天才会学的函数
# 缓存文件的原理
#
# 同时打开多个文件
with open('test.py', 'r', encoding='utf8') as fr,
open('test_swap.py', 'w', encoding='utf8') as fw:
data = fr.read()
data = data.replace('sb', '傻逼')
fw.write(data)
import os
os.remove('test.py')
os.rename('test_swap.py', 'test.py')
with open('test.py', 'r', encoding='utf8') as fr,
open('test_swap.py', 'w', encoding='utf8') as fw:
# 再大的文件都能修改
for i in fr:
s = '傻逼'
i = i.replace('sb', s)
fw.write(i)
# fw.flush() # 先保存成功再继续运行
#import os
os.remove('test.py')
os.rename('test_swap.py', 'test.py')