--学生クラスの設計-属性:名前、学生番号、年齢、成績-クラスクラスの設計-属性:クラス番号、すべての学生-要件:クラスへの学生の追加、学生の削除、学生の表示、指定した条件によるソートの実装
3874 ワード
python
#
class Student:
def __init__(self, name, num, age, score):
self.name = name
self.num = num
self.age = age
self.score = score
def __str__(self):
return ' :{} :{} :{} :{}'.format(self.name, self.num, self.age, self.score)
#
class Class:
def __init__(self, name):
self.name = name
self.stu_list = []
self.stu_dict = {}
#
def add_stu(self, stu):
self.stu_list.append(stu)
self.stu_dict[stu.num] = stu
#
def del_stu(self, num):
#
s = self.stu_dict.pop(num)
#
self.stu_list.remove(s)
#
def sort_stu(self, key=None, reverse=False):
self.stu_list.sort(key=key, reverse=reverse)
#
def get_stu(self, num):
return self.stu_dict.get(num)
#
def show_stu(self):
for s in self.stu_list:
print(s)
from random import randint
#
c = Class('ZZ-JY-PY1805')
#
for i in range(10):
name = 'stu' + str(i+1)
num = 100 + i
age = randint(20, 30)
score = randint(0, 100)
#
stu = Student(name, num, age, score)
#
c.add_stu(stu)
#
c.sort_stu(key=lambda s: s.score, reverse=True)
#
c.show_stu()
s = c.get_stu(104)
print(s)