学生选课习题
学生:
信息:学号,姓名,住址,选课列表
功能:
查看学生所有课程信息
添加课程:将选好的课程添加到课程列表中
课程
信息:课程编号,课程名称,老师
功能:
查看课程的全部信息
设置老师
老师
信息:老师编号,老师姓名,老师电话
要求:创建6个老师,6门课程,30个学生,给每个学生添加三门课
1 from random import randint 2 3 class Stu(object): 4 def __init__(self,id,name,address): 5 self.id = id 6 self.name = name 7 self.address = address 8 self.coures = [] 9 def add_coures(self,coures): 10 self.coures.append(coures) 11 def show(self): 12 print("学生:%s选的课程:" %self.name) 13 for i in self.coures: 14 print("课程名:%s" %i.name) 15 16 # def __str__(self): 17 # print("学生:%s选的课程:" % self.name) 18 # for i in self.coures: 19 # print("课程名:%s" % i.name) 20 21 22 class Coures(object): 23 def __init__(self,id,name,teacher=None): 24 self.id = id 25 self.name = name 26 self.teacher = teacher 27 def set_tea(self,teacher): 28 self.teacher = teacher 29 def show_coures(self): 30 if self.teacher: 31 print("%s课的老师是%s" %(self.name,self.teacher)) 32 else: 33 print("%s课的老师是%s" % (self.name, "无")) 34 35 class Teacher(object): 36 def __init__(self,id,name,phone): 37 self.id = id 38 self.name = name 39 self.phone = phone 40 41 t1 = Teacher(1,"shuai",1) 42 t2 = Teacher(2,"aoli",2) 43 t3 = Teacher(3,"deng",3) 44 t4 = Teacher(4,"xiang",4) 45 t5 = Teacher(5,"liu",5) 46 t6 = Teacher(6,"chuang",6) 47 48 c1 = Coures(1,"语文",t1) 49 c2 = Coures(2,"数学",t2) 50 c3 = Coures(3,"生物",t3) 51 c4 = Coures(4,"物理",t4) 52 c5 = Coures(5,"化学",t5) 53 c6 = Coures(6,"英语",t6) 54 c_list = [c1,c2,c3,c4,c5,c6] 55 stu_list = [] 56 for i in range(30): 57 stu = Stu(i,"stu%s"%i,"这个屯") 58 s = set() 59 while len(s) <3: 60 t = randint(0,5) 61 s.add(t) 62 for i in range(3): 63 stu.add_coures(c_list[list(s)[i]]) 64 stu_list.append(stu) 65 for stu in stu_list: 66 stu.show()