# 28.1 练习题
# 选课系统
# 要求
创建一个文件里面存储着学生的信息:姓名|年龄|性别|密码|权限
创建一个文件里面存储着课程信息
整个系统分有二种权限,老师 | 学生
老师拥有的权限:
- 可以对学生账号进行增删改查
- 可以对课程进行增删改查
学生拥有的权限:
- 查看老师信息
- 查看全部课程
- 进行课程学习(黑屏就可)
需要判断身份,并且根据身份来实例化对象
根据每个身份对应的类,让用户选择能做的事
# 实现
import os,sys
## 老师操作类
class teacher:
run_list = [
("学生信息操作平台", "student"),
("课程信息操作平台", "course")
]
@staticmethod
def student():
student_list = [
("增加学生信息", "student_add"),
("删除学生信息", "student_delete"),
("修改学生信息", "student_chang"),
("查看学生信息", "student_check")
]
while 1:
for i, n in enumerate(student_list, 1):
print("%s:%s" % (i, n[0]))
print("=" * 24)
print("友谊提醒:按Q返回上一层")
name = input("请输入你要操作的编号:")
print("=" * 24)
if name.lower() == "q":
break
file = sys.modules['__main__']
student_get = getattr(file,"teacher")
getattr(student_get,student_list[int(name)-1][1])()
print("=" * 24)
@staticmethod
def course():
global course_list
course_list = [
("增加课程信息", "course_add"),
("删除课程信息", "course_delete"),
("修改课程信息", "course_chang"),
("查看课程信息", "course_check")
]
while 1:
for i, n in enumerate(course_list, 1):
print("%s:%s" % (i, n[0]))
print("=" * 24)
print("友谊提醒:按Q返回上一层")
name = input("请输入你要操作的编号:")
print("=" * 24)
if name.lower() == "q":
break
file = sys.modules['__main__']
student_get = getattr(file, "teacher")
getattr(student_get, course_list[int(name) - 1][1])()
print("=" * 24)
@staticmethod
def student_check():
with open("student",mode="r",encoding="utf-8") as f:
for i in f:
if i.count("teacher") == 0:
name,age,sex,s,e = i.split("|")
print("姓名:%s \t 年龄:%s \t 性别:%s" %(name,age,sex))
@staticmethod
def student_delete():
name = input("请输入要删除的学生姓名:")
with open("student", mode="r", encoding="utf-8") as f , open("student_out", mode="w", encoding="utf-8") as g :
for i in f:
if i.count(name) == 0:
g.write(i)
os.remove("student")
os.rename("student_out","student")
@staticmethod
def student_chang():
name = input("请输入要修改的学生姓名:")
chang_former = input("请输入要修改的原内容:")
chang = input("请输入要修改的新内容:")
with open("student", mode="r", encoding="utf-8") as f, open("student_out", mode="w", encoding="utf-8") as g:
for i in f:
if i.count(name) != 0:
f_chang = i.replace(chang_former,chang)
g.write(f_chang)
else:
g.write(i)
os.remove("student")
os.rename("student_out", "student")
@staticmethod
def student_add():
name = input("学生姓名:")
age = input("学生年龄:")
sex = input("学生性别:")
passwd = input("学生账号密码:")
with open("student", mode="a", encoding="utf-8") as f:
f.write("\n")
f.write("%s|%s|%s|%s|student" %(name,age,sex,passwd))
@staticmethod
def course_check():
with open("course",mode="r",encoding="utf-8") as f:
for i in f:
print(i.strip())
@staticmethod
def course_delete():
name = input("请输入要删除的课程:")
with open("course", mode="r", encoding="utf-8") as f, open("course_out", mode="w", encoding="utf-8") as g:
for i in f:
if i.count(name) == 0:
for n in i.split():
g.write(n)
os.remove("course")
os.rename("course_out", "course")
@staticmethod
def course_chang():
name = input("请输入要修改的课程:")
chang_former = input("请输入要修改的原内容:")
chang = input("请输入要修改的新内容:")
with open("course", mode="r", encoding="utf-8") as f, open("course_out", mode="w", encoding="utf-8") as g:
for i in f:
if i.count(name) != 0:
f_chang = i.replace(chang_former, chang)
g.write(f_chang)
else:
g.write(i)
os.remove("course")
os.rename("course_out", "course")
@staticmethod
def course_add():
name = input("课程名称:")
with open("course", mode="a", encoding="utf-8") as f:
f.write("\n")
f.write(name)
## 学生操作类
class student:
run_list = [
("查看老师信息", "teacher"),
("查看全部课程", "course"),
("进入课程学习", "study")
]
@staticmethod
def teacher():
with open("student",mode="r",encoding="utf-8") as f:
for i in f:
if i.count("student") == 0:
name,age,sex,s,e = i.split("|")
print("姓名:%s \t 年龄:%s \t 性别:%s" %(name,age,sex))
@staticmethod
def course():
with open("course",mode="r",encoding="utf-8") as f:
for i in f:
print(i.strip())
@staticmethod
def study():
name = input("请输入要进行学习的课程名:")
print("=" * 48)
print("=" * 10, "教学视频播放中。。。。。。", "=" * 10)
print("=" * 48)
quit()
## 登录判断函数
def login():
name = input("请输入用户名(姓名):")
passwd = input("请输入密码:")
register = 0
with open("student", mode="r", encoding="utf-8") as f:
for i in f:
namea,age,sex,pawd,en = i.strip().split("|")
if name == namea and passwd == pawd:
s,e = name,en
register = 1
if register == 1:
print("登录成功")
return s,e
else:
print("登录失败")
quit()
## 程序运行函数
def run():
print("欢迎进入选课系统............")
print("请登录...")
s,e = login()
print("=" * 24)
print("%s,你的身份是:%s" %(s,e))
print("=" * 24)
print("进入%s操作平台......" %(e))
file = sys.modules['__main__']
run_list = getattr(file,e).run_list
print("="*24)
while 1:
for i,n in enumerate(run_list,1):
print("%s:%s" % (i, n[0]))
print("=" * 24)
print("友谊提醒:按Q退出")
name = input("请输入你要操作的编号:")
print("=" * 24)
if name.lower() == "q":
quit()
get = getattr(file,e)
run_get = run_list[int(name)-1]
getattr(get,run_get[1])()
print("=" * 24)
run()
代码整体还可以在优化