def main():
flag = open("flag.txt")
word = flag.read()
if word == "":
print("首次启动!")
flag.close()
file = open("flag.txt", "w") # 覆盖写模式,以重写的方式打开文件flag
file.write("1") # 将“1”写入falg文件中
file.close()
global admi
admi = {"root": "1234"}
global user
user = {}
print_login_menu()
user_select()
elif word[0] == "1":
print("欢迎回来!")
print_login_menu()
user_select()
else:
print("初始化参数错误!")
# 打印菜单
def print_login_menu():
print("----用户选择----")
print("1-管理员登录")
print("2-普通用户登录")
print("----------------")
# 用户选择
def user_select():
while True:
user_type_select = input("请选择用户类型:")
if user_type_select == "1":
root_login()
break
elif user_type_select == "2":
while True:
select = input("是否需要注册?(y/n):")
if select == "y" or select == "Y":
user_register()
user_login()
break
elif select == "n" or select == "N":
user_login()
break
else:
print("输入有误,请重新选择")
else:
print("输入有误,请重新选择")
# 管理员登录
def root_login():
global admi
while True:
print()
print("----管理员登陆----")
admi_id = input("请输管理员名称:")
admi_pwd = input("请输入密码:")
if (admi_id, admi_pwd) in list(admi.items()):
print("管理员登录成功!")
break
else:
print("管理员登录失败,请重新登录。")
# 用户注册
def user_register():
print()
print("----用户注册----")
user_id = input("请输入账户名:")
user_pwd = input("请输入密码:")
global user
user[user_id] = user_pwd
# 用户登录
def user_login():
while True:
print()
print("----用户登录----")
user_id = input("请输入账户名:")
user_pwd = input("请输入密码:")
global user
pwd = user.get(user_id, 0)
if pwd == 0:
print("查无此人!")
elif pwd == user_pwd:
print("登录成功!")
break
else:
print("密码不正确,请重新登录。")
admi = {}
user = {}
main()