1. 程式人生 > 其它 >面向物件第三次作業

面向物件第三次作業

技術標籤:筆記python

第一題(30分)

資料如下:

stu1.txt 孫同學,2020-5-21,20,'男',77,56,77,76,92,58,-91,84,69,-91
stu2.txt 趙同學,2020-11-3,24,'女',65,68,72,95,-81,71,86,91,57,91
stu3.txt 王同學,2021-8-7,25,'男',87,78,90,-76,88,47,100,65,69,100 
stu4.txt 李同學,2021-8-10,29,'男',92,54,85,71,-91,68,77,68,95,95

以上四個txt文件在work路徑下可以找到。

定義Student類,包括name、dob、age、gender和score屬性,包括top3方法用來返回學生的最大的3個成績(可重複)、sanitize方法用來將負的分數變為正的分數,負的分數可能是輸入錯誤。宣告stu_list物件組數用於儲存所有的學生物件。最後輸出所有的學生資訊包括姓名、生日、年齡、性別、最高的3個分數。

第一題的輸出結果如下,供參考:

def get_coach_data(filename): 
    with open(filename) as f: 
        line = f.readline() 
    return line.strip().split(',')

class Student:
    def __init__(self,a_name,a_dob,a_age, gender,a_scores=[]):
        self.name = a_name
        self.dob = a_dob
        self.age=a_age
        self.
gender=gender self.scores=a_scores def top3(self): return sorted(set([self.sanitize(t) for t in self.scores]))[0:3] def sanitize(self,scores_string): scores_string= scores_string.strip('-') target = int(scores_string) return (target)
stu1 = get_coach_data(
'work/stu1.txt') stu1 = Student(stu1.pop(0),stu1.pop(0),stu1.pop(0),stu1.pop(0),stu1) stu2 = get_coach_data('work/stu2.txt') stu2 = Student(stu2.pop(0),stu2.pop(0),stu2.pop(0),stu2.pop(0),stu2) stu3 = get_coach_data('work/stu3.txt') stu3 = Student(stu3.pop(0),stu3.pop(0),stu3.pop(0),stu3.pop(0),stu3) stu4 = get_coach_data('work/stu4.txt') stu4 = Student(stu4.pop(0),stu4.pop(0),stu4.pop(0),stu4.pop(0),stu4) stu_lis=[stu1, stu2, stu3, stu4] for stu in stu_lis: print("姓名: {} 生日:{} 年齡: {} 性別:{} 分數: {}".format(stu.name, stu.dob, stu.age, stu.gender, stu.top3()))
姓名: 孫同學 生日:2020-5-21 年齡: 20 性別:'男' 分數: [56, 58, 69]
姓名: 趙同學 生日:2020-11-3 年齡: 24 性別:'女' 分數: [57, 65, 68]
姓名: 王同學 生日:2021-8-7 年齡: 25 性別:'男' 分數: [47, 65, 69]
姓名: 李同學 生日:2021-8-10 年齡: 29 性別:'男' 分數: [54, 68, 71]

第二題(30分)

資料格式如下:

stu5.txt 特長同學,2020-10-5,20,'男',180,87,98,77,76,92,58,-76,84,69,-47
stu6.txt 特長同學,2020-10-6,20,'女',230,76,48,82,88,92,58,-91,84,69,-68

以上兩個txt文件在work路徑下可以找到。

定義Spostudent、Artstudent為Student的子類,在子類的屬性裡面新增了spe為特長分數。Spostudent包括的top3方法返回的是最低的3個得分(可重複),Artstudent包括top3方法返回的是最高的3個得分(可重複),最後使用多型的方式輸出2個特長同學的姓名、生日、年齡、性別、分數、特長分。

第二題的輸出結果如下,供參考:

class Spostudent(Student):
    def __init__(self,a_name,a_dob,a_age, gender,techang,a_scores=[]):
        Student.__init__(self,a_name,a_dob,a_age, gender,a_scores)
        self.techang=techang

class Artstudent(Student):
    def __init__(self,a_name,a_dob,a_age, gender,techang,a_scores=[]):
        Student.__init__(self,a_name,a_dob,a_age, gender,a_scores)
        self.techang=techang        
    def top3(self):
        target=[self.sanitize(t) for t in self.scores]
        target.sort(reverse=True)
        return target[0:3]

    def sanitize(self,scores_string):
        scores_string= scores_string.strip('-')
        target = int(scores_string)
        return (target)       

stu5 = get_coach_data('work/stu5.txt')
stu5 = Spostudent(stu5.pop(0),stu5.pop(0),stu5.pop(0),stu5.pop(0),stu5.pop(0),stu5)

stu6 = get_coach_data('work/stu6.txt')
stu6 = Artstudent(stu6.pop(0),stu6.pop(0),stu6.pop(0),stu6.pop(0),stu6.pop(0),stu6)

stu_lis=[stu5, stu6]
for stu in stu_lis:
    print("姓名: {} 生日: {} 年齡: {} 性別:{} 分數: {} 特長分:{}".format(stu.name, stu.dob, stu.age, stu.gender, stu.top3(), stu.techang ))
姓名: 特長同學 生日: 2020-10-5 年齡: 20 性別:'男' 分數: [56, 58, 69] 特長分:180



---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-7-4e906dead606> in <module>
     26 stu_lis=[stu5, stu6]
     27 for stu in stu_lis:
---> 28     print("姓名: {} 生日: {} 年齡: {} 性別:{} 分數: {} 特長分:{}".format(stu.name, stu.dob, stu.age, stu.gender, stu.top3(), stu.techang ))


<ipython-input-7-4e906dead606> in top3(self)
      9         self.techang=techang
     10     def top3(self):
---> 11         target=set([self.sanitize(t) for t in self.scores])[0:3]
     12         target.sort(reverse = True)
     13         return


TypeError: 'set' object is not subscriptable

請點選此處檢視本環境基本用法.

Please click here for more detailed instructions.