课后习题:
学生年龄作业
class Person:
def __init__(self,name,age):
self.name=name
self._age=age
@property
def age(self):
print(self._age)
if 0<=self._age<=18:
return '未成年'
elif 19<=self._age<=35:
return '青年'
elif 36<=self._age<=50:
return '中年'
elif 51 <= self._age<= 70:
return '中老年'
elif 71<=self._age<=150:
return '老年'
@age.setter
def age(self,value):
if not isinstance(value,int):
raise ValueError
if not 0<=value<=150:
raise ValueError
self._age=value
p1=Person('jeff',34)
print(p1.age)
p1.age=189
print(p1.age)
day14作业
1..定义一个学生类。有属性:姓名、年龄、成绩(语文,数学,英语)[每课成绩的类型为整数]
方法: a. 获取学生的姓名:getname() b. 获取学生的年龄:getage()
c. 返回3门科目中最高的分数。get_course()
import random
class Student:
def __init__(self,name,age,scores=[]):
self.name=name
self.age=age
self._scores=scores
@property
def scores(self,value):
if not isinstance(value,int):
raise ValueError
if not 0<=len(self._scores)<=2:
raise ValueError
language_score=self._scores[0]
maths_score=self._scores[1]
englsih_score=self._scores[2]
value=[language_score,maths_score,englsih_score]
self._scores=value
def get_name(self):
return print(self.name)
def get_age(self):
return print(self.age)
def get_course(self):
language_score = self._scores[0]
maths_score = self._scores[1]
englsih_score = self._scores[2]
self._scores = [language_score, maths_score, englsih_score]
print(max(self._scores))
stu1=Student('sara',21,[34,54,64])
stu2=Student('ann',21,[78,67,89])
stu1.get_age()
stu1.get_name()
stu1.get_course()
stu2.get_course()
第五题写歌词不会