Day13--课后作业

  1. 声明一个电脑类
    属性:品牌、颜色、内存大小
    方法:打游戏、写代码、看视频
    a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
    b.通过attr相关方法去获取、修改、添加和删除它的属性

class Computer:
    def __init__(self,brand,color,ram):
        self.brand=brand
        self.color=color
        self.ram=ram

    def play_game(self):
        print("play game")

    def write_code(self):
        print("write code")

    def view_video(self):
        print("view video")

    def __str__(self):
        return str(self.__dict__)

computer_one=Computer('Acer','black','4G')
#a.对象点的方式获取、修改、添加和删除它的属性
print(computer_one) #{'brand': 'Acer', 'color': 'black', 'ram': '4G'}
computer_one.brand="Lenovo"
computer_one.color="yellow"
computer_one.ram="8G"
print(computer_one) #{'brand': 'Lenovo', 'color': 'yellow', 'ram': '8G'}
computer_one.storage="1T"
print(computer_one) #{'brand': 'Lenovo', 'color': 'yellow', 'ram': '8G', 'storage': '1T'}
del computer_one.color
print(computer_one) #{'brand': 'Lenovo', 'ram': '8G', 'storage': '1T'}
#b.通过attr相关方法去获取、修改、添加和删除它的属性
computer_two=Computer('Acer','black','4G')
a=getattr(computer_two,"brand")
b=getattr(computer_two,"color")
c=getattr(computer_two,"ram")
print(a,b,c) #Acer black 4G
setattr(computer_two,"brand","Lenovo")
setattr(computer_two,"color","blue")
setattr(computer_two,"ram","8G")
print(computer_two) #{'brand': 'Lenovo', 'color': 'blue', 'ram': '8G'}
delattr(computer_two,'brand')
print(computer_two)#{'color': 'blue', 'ram': '8G'}
setattr(computer_two,'strorage','2T')
print(computer_two)#{'color': 'blue', 'ram': '8G', 'strorage': '2T'}

2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、 年龄、狗
人的方法:遛狗
a.创建人的对象名字叫小明,让他拥有一条狗 ,然后让小明去遛狗

class Dog:

    def __init__(self,name,age,color):
        self.name=name
        self.age=age
        self.color=color

    def barking(self):
        print("Dog barking")


class Person:

    def __init__(self,name,age,dog):
        self.name=name
        self.age=age
        self.dog=dog

    def walking_the_dog(self,dog):
        print("%s 正在遛一只名字为%s,颜色为%s的狗"% (self.name,dog.name,dog.color))
        dog.barking()


dog1=Dog("沙皮",2,"黄色")
p1=Person("小明",20,dog1)
p1.walking_the_dog(dog1)
'''
小明 正在遛一只名字为沙皮,颜色为黄色的狗
Dog barking
'''

3.声明一个矩形类:
属性: 长、宽
方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积

class Rectangle:
    def __init__(self,length,width):
        self.length=length
        self.width=width

    def perimeter(self):
        return (self.length + self.width)*2

    def area(self):
        return self.length*self.width

rec1=Rectangle(20,30)
print(rec1.perimeter(),rec1.area())
rec2=Rectangle(10,20)
print(rec2.perimeter(),rec2.area())

4.创建一个学生类:
属性:姓名,年龄,学号,成绩
方法:答到,展示学生信息
创建一个班级类: 属性:学生,班级名
方法:添加学生,删除学生,点名, 获取班级中所有学生的平均值, 获取班级中成绩最好的学生

class Student:
    def __init__(self, name, age, id,score):
        self.name = name
        self.age = age
        self.id = id
        self.score=score

    def answer(self):
        print(self.__dict__)

    def __str__(self):
        return str(self.__dict__)

class Class:
    def __init__(self, name, students:list):
        self.name = name
        self.students = students

    def add_student(self,stu:Student):
        self.students.append(stu)

    def del_student(self,stu:Student):
        self.students.remove(stu)

    def call_student(self,stu:Student):
        stu.answer()


    def average_score(self):
        sum=0
        average=0
        for stu in self.students:
            sum+=stu.score
        average=sum/len(self.students)
        return average

    def best_student(self):
        students.sort(key= lambda item:item.score,reverse=True)
        return students[0].name

    def __str__(self):
        return str(self.__dict__)

stu1 = Student("小明", 22, "stu01", 98)
stu2 = Student("小李",24,"stu02",89)
stu3 = Student("小赵",28,"stu03",78)
stu4 = Student("小张",26,"stu04",93)
stu5 = Student("小王",22,"stu05",86)
stu6=Student("小钱",26,"stu05",95)
students=[stu1,stu2,stu3,stu4,stu5]
python_class=Class("python1807",students)
print(python_class)
stu6=Student("小钱",26,"stu05",95)
#添加学生
python_class.add_student(stu6)
print(python_class)
#删除学生
python_class.del_student(stu6)
print(python_class)
#求平均成绩
print(python_class.average_score())
#最优秀的学生
print(python_class.best_student())
#点名
python_class.call_student(stu1)
'''
结果为:
{'name': 'python1807', 'students': [<__main__.Student object at 0x000002421A404B00>, <__main__.Student object at 0x000002421A404B38>, <__main__.Student object at 0x000002421A404B70>, <__main__.Student object at 0x000002421A404BA8>, <__main__.Student object at 0x000002421A404BE0>]}
{'name': 'python1807', 'students': [<__main__.Student object at 0x000002421A404B00>, <__main__.Student object at 0x000002421A404B38>, <__main__.Student object at 0x000002421A404B70>, <__main__.Student object at 0x000002421A404BA8>, <__main__.Student object at 0x000002421A404BE0>, <__main__.Student object at 0x000002421A404D68>]}
{'name': 'python1807', 'students': [<__main__.Student object at 0x000002421A404B00>, <__main__.Student object at 0x000002421A404B38>, <__main__.Student object at 0x000002421A404B70>, <__main__.Student object at 0x000002421A404BA8>, <__main__.Student object at 0x000002421A404BE0>]}
88.8
小明
{'name': '小明', 'age': 22, 'id': 'stu01', 'score': 98}

'''

方法二:

import  random
class Student:
    
    gen_state=(x % 2 for x in range(1,101) )  #状态生成器
    gen_id=("python"+str(x).rjust(3,"0") for x in range(1,101)) #学号生成器
    
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.id = self.__class__.gen_id.__next__()
        self.score=random.randint(1, 101)
        self.state=self.__class__.gen_state.__next__()
      
    def answer(self):
        if self.state:
            print("%s 到!"% self.name)
        else:
            print("%s 缺席!"% self.name)

    def show_info(self):
        print(self.__dict__)

    def __repr__(self):
        return str(self.__dict__)

class PythonClass:

    def __init__(self, name, students: list):
        self.name = name
        self.students = students

    def add_student(self):
        name=input('姓名:')
        age = input("年龄:")
        stu=Student(name,age)
        self.students.append(stu)
        print("添加成功:",end="")
        stu.show_info()
        return stu

    def del_student(self):
        del_name=input("姓名:")
        lenth=len(self.students.copy())
        for stu in self.students.copy():#若不是副本,元素在减少,删除不完
            if stu.name==del_name:
                self.students.remove(stu)
        if lenth!=len(self.students):
            print("删除成功")

    def call_student(self):
        for stu in self.students:
            stu.answer()

    def average_score(self):
        sum = 0
        average = 0
        for stu in self.students:
            sum += stu.score
        average = sum / len(self.students)
        return average

    def best_student(self):
        self.students.sort(key=lambda item: item.score, reverse=True)
        return self.students[0].name

python=PythonClass("Python1807",[])

stu1=python.add_student()
stu2=python.add_student()
stu3=python.add_student()
stu4=python.add_student()

python.call_student()
print(python.best_student())
"""
结果为:
姓名:xiaoming
年龄:22
添加成功:{'name': 'xiaoming', 'age': '22', 'id': 'Python001', 'score': 66, 'state': 1}
姓名:xiaohuang
年龄:24
添加成功:{'name': 'xiaohuang', 'age': '24', 'id': 'Python002', 'score': 94, 'state': 0}
姓名:xiaozhang
年龄:25
添加成功:{'name': 'xiaozhang', 'age': '25', 'id': 'Python003', 'score': 22, 'state': 1}
姓名:xiaohong
年龄:27
添加成功:{'name': 'xiaohong', 'age': '27', 'id': 'Python004', 'score': 9, 'state': 0}
xiaoming 到!
xiaohuang 缺席!
xiaozhang 到!
xiaohong 缺席!
xiaohuang
"""
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容

  • 如果我即将在这个世界上消失,会不会有人记得我?如果连这样的人,都没有,生命那么漫长,我拿什么来拯救孤独?
    七月悠悠阅读 152评论 0 0
  • 假如你蹙眉头 我愿低下头来 吻你的双眸 假如你低下头 我愿昂起头来 吻你的艳唇 假如你离去了 我愿追随着你 直到天荒地老
    Ling_00阅读 143评论 0 2
  • 不是自恋,是这条件,都能配上![捂脸] 灵魂伴侣,就咱这雌雄同体的人,自备齐!不需要![偷笑] 别人都懂,只是想懂...
    纵情嬉戏天地间阅读 299评论 2 0
  • 公司项目需要做一个视频下载功能,很简单,一次只需要下载一个视频,不需要同时下载多个视频,唯一的需求是支持断点续传。...
    ThaiLanKing阅读 2,339评论 2 5