def register():
while 1:
import os
check = input("退出注册请按Q或q")
if check.upper() == "Q":
break
else:
username = input("请输入用户名:")
password = input("请输入用户名密码(禁止使用“_”) :")
with open("11.txt", mode="r", encoding="utf-8") as f1,\
open("11.txt_副本", mode="w", encoding="utf-8") as f2:
count = 1
for line in f1:
f2.write(line)
s = line.strip().split("_")
if username == s[0]:
count = count +1
if count > 1:
print("用户名已被注册")
else:
f2.write(username + "_" + password + "\n")
os.remove("11.txt")
os.rename("11.txt_副本", "11.txt")

def login():
while 1:
check = input("退出登录请按Q或q:")
if check.upper() == "Q":
break
else:
username = input("请输入用户名:")
password = input("请输入密码:")
with open("11.txt", mode="r", encoding="utf-8") as f:
count = 0
for line in f:
u, p = line.strip().split("_")
if username == u and password == p:
count = count +1
if count == 1:
print("登录成功")
else:
print("密码或用户名错误,请重新登录!")
register()
login()
01-03 14:16