有没有大佬能帮我改一下代码,让我在使用装饰函数的前提下可以登陆
#能登陆
#能注册
#能退出
#能查已经报名的科目
#能报名
import time
course_dict={
'1':'高等数学',
'2':'photoshop的使用',
'3':'C语言的学习',
'4':'面向对象程序设计',
'5':'心理课',
'6':'Unity游戏制作',
}
course_list={}
id_dict={
'1':'student',
'2':'teacher',
'3':'administrator',
}
ids='''
1:student
2:teacher
3:administrator
'''
username_list=[]
def sanceng(role):
def login_deco(func):
def wrapper(*args, **kwargs):
print(f'站住!表明你的身份!{ids}')
role_inp = input('请输入你的身份id:')
role = id_dict[role_inp]
if username_list:
print('已经登录,请勿重复登录')
res = func(*args, **kwargs)
return res
username_inp = input('请输入用户名:')
pwd_inp = input('请输入密码:')
with open(f'{role}_info.txt', 'r', encoding='utf8') as fr:
for user_info in fr:
username, pwd = user_info.strip().split(':')
if username_inp == username and pwd_inp == pwd:
if role=='student':
print(f'欢迎{username_inp}同学')
if role=='teacher':
print(f'欢迎{username_inp}老师')
if role=='administrator':
print(f'欢迎{username_inp}大哥')
username_list.append(username)
res = func(*args, **kwargs)
return res
else:
print('登录失败')
return wrapper
return login_deco
def inp_username_pwd():
username_inp=input('请输入你的用户名')
pwd_inp=input('请输入你的密码')
return username_inp,pwd_inp
def login():
sanceng('admin')
# def login():
# print('欢迎来到登陆系统')
# if username_list:
# print("你已经登陆了哦,别再登陆了")
# return
#
# count=0
# while count<3:
# username_inp,pwd_inp=inp_username_pwd()
#
# with open('userinfo.txt','r',encoding='utf8') as fr:
# for use_info in fr:
# use_info=use_info.strip()
# username,pwd=use_info.split(':')
#
#
# if username==username_inp and pwd==pwd_inp:
# print("登陆中", end="")
# for i in range(5):
# print(".", end='')
# time.sleep(0.2)
# print(".")
# print('成功登陆')
# username_list.append(username_inp)
# return
# else:
# print('由于一些原因,登陆失败了')
# count+=1
def register():
print('欢迎来注册')
print(f'站住!表明你的身份!{ids}')
role_inp = input('请输入你的身份id:')
role = id_dict[role_inp]
username_inp,pwd_inp=inp_username_pwd()
with open (f'{role}_info.txt','a',encoding='utf8')as fa:
fa.write(f'{username_inp}:{pwd_inp}
')
def check():
if not username_list:
print("请登陆之后再来哦")
return
print('欢迎来看报名情况呀')
print("正在进入选课查看", end="")
for i in range(5):
print(".", end='')
time.sleep(0.2)
print(".")
with open('choosed_course','r',encoding='utf8')as fr:
data=fr.read()
print(data)
print('
')
def report():
print("你来选课啦!要选两门课哦!")
if not username_list:
print("请登陆之后再来哦")
return
print('''
1.高等数学
2.photoshop的使用
3.C语言的学习
4.面向对象程序设计
5.心理课
6.Unity游戏制作
''')
choose_count=0
while choose_count<2:
course_choice=input("请输入你想要选择的科目的数字")
if course_choice not in course_dict:
print('请输入列表里有的科目哦')
continue
course_name=course_dict[course_choice]
if course_name in course_list:
course_list[course_name]+=1
choose_count+=1
else:
course_list[course_name]=1
choose_count+=1
print(f'成功选择{course_name},你一共选择了{course_list}')
with open('choosed_course','a',encoding='utf8')as fa:
fa.write(f'{username_list}:{course_name}
')
func_dice={
'1':login,
'2':register,
'3':check,
'4':report,
'q':quit,
}
while True:
print('''
1 登陆
2 注册
3 查看报了啥
4 选课
q 退出
''')
func_choice=input('请输入你想要进行的操作(输入q退出):')
if func_choice=='q':
print("正在退出", end="")
for i in range(5):
print(".", end='')
time.sleep(0.2)
print(".")
break
if func_choice not in func_dice:
print("你看不懂中国字吗")
continue
func_dice[func_choice]()