题目:作业:
# 做一个注册登录系统
# input("请输入要使用的功能(1-3)")
# 1.注册 (用户输入 账户名 密码 确认密码 如果前后两次密码一直则保存用户的账户和密码到account.txt中)
# 2.登陆 用户输入 账户名 密码 跟account.txt中的账户密码进行比对,如果相同则登录成功
# 3.退出 使用sys模块中方法
python代码实现:
import sys
def register():
username = input("请输入账户名:")
password = input("请输入密码:")
confirm_password = input("请再次输入密码:")
if password != confirm_password:
print("两次密码输入不一致,请重新注册!")
return
with open("account.txt", "a") as f:
f.write(username + " " + password + "\n")
print("注册成功!")
def login():
username = input("请输入账户名:")
password = input("请输入密码:")
with open("account.txt", "r") as f:
for line in f.readlines():
account = line.strip().split()
if account[0] == username and account[1] == password:
print("登录成功!")
return
print("账户名或密码错误,请重新登录!")
while True:
choice = input("请输入要使用的功能(1-3):")
if choice == "1":
register()
elif choice == "2":
login()
elif choice == "3":
sys.exit(0)
else:
print("输入错误,请重新输入!")
测试结果显示: