Python设计题

Python017

Python设计题,第1张

#!/usr/bin/python3

# -*- coding:utf-8 -*-

"""

@author:

@file :Classes.py

@time :2019/7/3 14:55

"""

"""

班级类、Person类、学生类、辅导员类

"""

class Person:

"""人员类"""

name = ''

age = 0

gender = ''

def __init__(self, n, a, g):

self.name = n

self.age = a

self.gender = g

class Student(Person):

"""学生类"""

studenno = ''

def __init__(self, n, a, g, s):

Person.__init__(self, n, a, g)

self.studenno = s

class Counsellor(Person):

"""辅导员类"""

employeeno = ''

def __init__(self, n, a, g, e):

Person.__init__(self, n, a, g)

self.employeeno = e

class ClassMath():

"""班级类"""

classname = ''

def __init__(self, cn):

self.classname = cn

self.student = Student

self.counsellor = Counsellor

按照题目要求编写的Python程序如下

class Grade(object):

def __init__(self,grade):

self.grade=grade

class Class(object):

def __init__(self,bclass):

self.bclass=bclass

class Teacher(Grade,Class):

def __init__(self,grade,bclass,subject,name):

Grade.__init__(self,grade)

Class.__init__(self,bclass)

self.subject=subject

self.name=name

def run(self):

print("老师的姓名%s,年级%s,班级%s,学科%s" %(self.name,self.grade,self.bclass,self.subject))

class Student(Grade,Class):

def __init__(self,grade,bclass,age,name):

Grade.__init__(self,grade)

Class.__init__(self,bclass)

self.age=age

self.name=name

def run(self):

print("学生的姓名%s,年龄%d,年级%s,班级%s" %(self.name,self.age,self.grade,self.bclass))

T=Teacher('五','3','数学','张三')

S=Student('四','2',10,'李四')

val=input("请输入打印老师(T),学生(S):")

if val=='T':

T.run()

else:

S.run()

源代码(注意源代码的缩进)

#创建班级分数字典,结构是字典套字典

class_scores = {

----'xiaoming':{'数分1':88,'数分2':89,'统计学':97},

----'lihua': {'数分1': 98, '数分2': 99,'统计学':99},

----'laowang': {'数分1': 68, '数分2': 59,'统计学':67}

}

def get_subject_score(subject):

#输入课名,打印成绩排名和平均分

----subject_score = {}

----for student in class_scores.keys():

--------subject_score[student]=class_scores[student][subject]

----subject_score = sorted(subject_score.items(),key=lambda x:x[1],reverse=True)

----all_score = 0

----for a in subject_score:

--------print(a)

--------all_score += a[1]

----print('平均分%.2f'%(all_score/len(subject_score)))

def get_student_scores(student):

#输入学生名,打印成绩排名和平均分

----student_score = class_scores[student]

----student_score = sorted(student_score.items(),key=lambda x:x[1],reverse=True)

----all_score = 0

---- for a in student_score:

--------print(a)

--------all_score += a[1]

----print('平均分%.2f'%(all_score/len(student_score)))

#获取统计学的成绩

get_subject_score('统计学')

#获取老王的成绩

get_student_scores('laowang')

#添加新同学

class_scores['新同学']={'数分1': 66, '数分2': 77,'统计学':88}

#添加新课程

python_scores ={'xiaoming':78,'lihua':100,'laowang':15,'新同学':99}

for student,score in python_scores.items():

----class_scores[student]['python'] = score

print(class_scores)

#输出老王的几何学成绩

print(class_scores['laowang']['几何学'])