写了一个简单的选课系统的代码,问题一堆,遇到pickle反序列化不能完全导出的问题,大神还烦解答下吧

#!/bin/bash/evn python
#* coding:utf-8 *
#Autior Dahai
import pickle

‘’‘定义三个函数,创建班级,创建课程和创建老师’’’

def creat_class():

classes_creat_name = input("请输入班级的姓名:")
with open("classes_creat.txt",'ab') as f:
    f.write(pickle.dumps(classes_creat_name))

def creat_course():
course_creat_name = input(“请输入课程名称:”)
with open(“course_creat.txt”,‘ab’) as f:
f.write(pickle.dumps(course_creat_name))

def creat_teachers():

teachers_creat_name = input("请输入老师姓名:")
teachers_creat_passwd=input("请输入密码---->>>")
teachers_creat_info={teachers_creat_name:teachers_creat_passwd}

with open("teachers_creat.txt",'ab') as f:
    f.write(pickle.dumps(teachers_creat_info))

‘’‘现有班级情况’’’
classes_python=[‘python精品班14期’,‘python冲刺班14期’,‘python基础班14期’]
classes_linux=[‘linux精品班14期’,‘linux冲刺班14期’,‘linux基础班14期’]
classes_go=[‘go精品班14期’,‘go冲刺班14期’,‘go基础班14期’]
‘’‘初始化班级’’’
def initialize_class():
with open(“course_creat.txt”,‘wb’) as f:
f.write(pickle.dumps(classes_python))
f.write(pickle.dumps(classes_linux))
f.write(pickle.dumps(classes_go))

‘’‘定义初始化函数,初始化后课程回归到刚开始状态’’’
def initialize():
with open(“classes_creat.txt”,‘wb’)as f:
f.write(pickle.dumps(classes_python))
f.write(pickle.dumps(classes_linux))
f.write(pickle.dumps(classes_go))

print("系统初始化已完成")

‘’‘定义学校’’’
class School(object):
def init(self,name,addr):
self.name=name
self.addr=addr
course_name=[]
classes_name=[]
teachers_name=[]

def tell(self):
    print('''
    -------info of %s--------
    school name:%s
    school address:%s
    school teaching:IT'''%(self.name,self.name,self.addr))

school1=School(‘老男孩教育’,‘北京天安门’)
school2=School(‘老男孩教育’,‘上海南京路’)
‘’‘定义班级’’’
class Clesses(School):
def index(self,name,addr,course_name,teachers_name):
super(Clesses, self).index(course_name,teachers_name)
def tell(self):
print(’’’
-------info of %s--------
classes name:%s
classes course:%s
classes teacher:%s
classes address:%s’’’ % (self.name, self.name, self.course_name,self.teachers_name,self.addr))

‘’‘定义课程’’’
class Course(object):
def init(self,name,period,cost,addr):
self.name=name
self.period=period
self.cost=cost
self.addr=addr

def tell(self):
    print('''
                   -------info of %s--------
                   course name:%s
                   course period:%s
                   classes cost:%s
                   classes address:%s''' % (self.name, self.name, self.period, self.cost,self.addr))

python=Course(‘python14期’,‘8个月’,12000,‘北京’)
linux=Course(‘linux13期’,‘6个月’,10000,‘北京’)
go=Course(‘go8期’,‘5个月’,9000,‘上海’)

‘’‘定义学生注册函数’’’
def enroll():
print(’--------欢迎进入学员注册界面--------’)
name = input(“请输入你的姓名”)
age = input(“请输入你的年龄”)
sex = input(“请输入你的性别”)
passwd = input(“请输入你的密码”)
date1=[name,age,sex,passwd]
with open(“学员信息.txt”, ‘ab’) as f:
f.write(pickle.dumps(date1))

‘’‘定义学生这个类:可以选择课程和班级’’’
class Student(object):
def init(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex
self.course = []
self.classes=[]

def tell(self):
    print('''
                          -------info of %s--------
                          student name:%s
                          student age:%s
                          student sex:%s
                          student course:%s
                          student classes:%s''' % (self.name, self.name, self.age, self.sex, self.course,self.classes))



def choice_course(self):             #进入学生选课函数
    print(school1.tell(), school2.tell())
    student_school_choice = input("请选择学校名")
    print("1-->")
    python.tell()
    print("2-->")
    linux.tell()
    print("3-->")
    go.tell()
    choice_name=input("请选择课程,输入对应编号")
    money=int(input('请输入金额'))
    if money == 12000:
        with open("classes_creat.txt", 'rb') as f:
            date = pickle.loads(f.readline())
            print(date)
        choice_classes = input("请选择班级")

        with open("学员课表.txt", 'ab') as f:
            f.write(pickle.dumps(choice_classes))
            f.write(pickle.dumps('\npython课程\n'))

    elif money == 10000:

        with open("classes_creat.txt", 'rb') as f:
            date = pickle.loads(f.readline())
            print(date)
        choice_classes = input("请选择班级")
        self.course.append('linux')
        self.classes = [choice_classes]

        with open("学员课表.txt", 'ab') as f:
            f.write(pickle.dumps(choice_classes))
            f.write(pickle.dumps(self.course))


    elif money == 9000:
        with open("classes_creat.txt", 'rb') as f:
            date = pickle.loads(f.readline())
            print(date)
        choice_classes = input("请选择班级")
        self.course.append('go')
        self.classes = [choice_classes]

        with open("学员课表.txt", 'ab') as f:
            f.write(pickle.dumps(choice_classes))
            f.write(pickle.dumps(self.course))

    with open("学员信息.txt", 'ab') as f:
        f.write(pickle.dumps(student_school_choice))
    print("恭喜您报名成功")

initialize()

st1=Student(‘zhansan’,25,‘man’)

st1.choice_course()

‘’‘主程序入口’’’
view={1:“管理员入口”,
2:“老师入口”,
3:“学生入口”}

print("-----------欢迎进入老男孩课程系统-----------")
while True:
print(“系统窗口如下:”)
print(view)

import_in=int(input("请选择系统窗口编号:----->>>"))
if import_in==1:
    admin=input("请输入管理员账号---->>>")
    passwd=input("请输入管理员密码---->>>")
    if passwd=='abc123':
        view_admin={1:"创建班级",
                    2:"创建老师",
                    3:"创建课程",
                    4:"查看学员信息",
                    5:"初始化班级"}


        print("欢迎进入管理界面")
        print(view_admin)
        admin_choice=int(input("------请选择操作编号---->>>>"))
        if admin_choice==1:
            creat_class()
        elif admin_choice==2:
            creat_teachers()
        elif admin_choice==3:
            creat_course()
        elif admin_choice==4:
            with open("学员信息.txt",'rb') as f:                                  ###后面补充学员信息情况
                date2=pickle.loads(f.read())
            print(date2)
        elif admin_choice==5:
            ok=input("此操作有风险,是否确认操作y/n:---->")
            if ok=="y":
                initialize()
                initialize_class()       ###初始化函数

            else:
                print("谢谢使用")
                pass
                                              #写程序时再加入主变量
elif import_in==2:
    with open("teachers_creat.txt", 'rb') as f:
        date=pickle.loads(f.readline())
        teacher_admin=input("请输入账号---->>")
        teacher_passwd=input("请输入密码---->>")
        if teacher_passwd==date[teacher_admin]:


            view_teacher={1:"查看班级信息",
                          2:"查看班级学员列表",
                          3:"修改密码"}
            print("---------欢迎进入教师入口--------")
            print(view_teacher)
            teacher_choice=int(input("请选择操作编号----->>>"))
            if teacher_choice==1:
                with open("course_creat.txt",'rb')as f:
                    info_ok=[]
                    for info in pickle.loads(f.read()):
                        info_ok.append(info)

                    print(info_ok)


            elif teacher_choice==2:
                pass                          ##创建完学员信息后补全

            elif teacher_choice==3:
                pass
elif import_in==3:
    enroll()
    with open("学员信息.txt", 'rb') as f:
        stu1_info=pickle.loads(f.readline())
    student1=Student(stu1_info[0],stu1_info[1],stu1_info[2])
    student1.choice_course()
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐