day15-作业

  1. 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法

    class Auto:
    
        def __init__(self, color, weight, speed=0, tire_num=4):
            self.tire_num = tire_num
            self.color = color
            self.weight = weight
            self.speed = speed
    
        def speed_up(self):
            print('踩油门!!')
            self.speed += 1
    
        def speed_down(self):
            if self.speed >= 1:
                print('踩刹车')
                self.speed -= 1
            else:
                print('汽车没启动')
    
        def stop(self):
            if self.speed >= 1:
                while True:
                    self.speed -= 10
                    if self.speed <= 0:
                        break
                print('靠边停车')
            else:
                print('汽车已经停了')
    
    
    class CarAuto(Auto):
        def __init__(self, color, weight, cd, air_condition, speed=0, tire_num=4):
            super().__init__(color, weight, speed, tire_num)
            self.cd = cd
            self.air_condition = air_condition
    
        def speed_up(self):
            print('踩油门')
            self.speed += 10
    
        def speed_down(self):
            print('踩刹车')
            if self.speed > 0:
                self.speed -= 10
                if self.speed <= 0:
                    print('汽车已经停车')
            else:
                print('汽车还未启动')
    
    
    c2 = CarAuto('bai', '1t', True, True, speed=3)
    print(c2.speed)
    car1 = Auto('黑色', '2t', 120)
    print(car1.speed)
    print(car1.color)
    print(car1.tire_num)
    print(car1.weight)
    car1.speed_down()
    print(car1.speed)
    car1.stop()
    

  2. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数

    class Person:
        num = 0
        def __init__(self):
            Person.num += 1
            
            
    p1 = Person()
    p2 = Person()
    print(Person.num)
    

  3. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,

    要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印

    class Animal:
        def __init__(self, gender, age, color, bread):
            self.gender = gender
            self.age = age
            self.color = color
            self.bread = bread
    
        def __repr__(self):
            return '{}的对象:性别-{} 年龄-{} 颜色-{} 品种-{}'.format(self.__class__.__name__, self.gender, self.age, self.color, self.bread)
    
    

  4. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值

    class Readonly(Exception):
        def __str__(self):
            return '该属性不能不能赋值'
    
    
    class Circle:
        pi = 3.1415926
    
        def __init__(self, radius):
            self.radius = radius
            self._area = Circle.pi**2*radius
            self._perimeter = 2*Circle.pi*radius
    
        @property
        def area(self):
            return self.area
    
        @area.setter
        def area(self, value):
            raise Readonly
    
        @property
        def perimeter(self):
            return self.perimeter
    
        @perimeter.setter
        def perimeter(self, value):
            raise Readonly
    

  5. 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)

    from random import shuffle
    
    class Poker:
        num = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
        colors = '♠♥♦♣'
        cards = ['大王', '小王']
        for i in num:
            for j in colors:
                cards.append(j+i)
    
        @classmethod
        def shuffle(cls):
            shuffle(cls.cards)
    
        @classmethod
        def deal(cls):
            User1 = []
            User2 = []
            User3 = []
            i = 0
            cards = iter(cls.cards)
            for _ in range(51):
                i += 1
                if i == 1:
                    User1.append(iter(cards).__next__())
                elif i == 2:
                    User2.append(iter(cards).__next__())
                elif i == 3:
                    User3.append(iter(cards).__next__())
                    i = 0
    
            return User1, User2, User3
    
    

  6. (尝试)写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式进行存储的。歌词前面对应的是时间,在对应的时间点可以显示对应的歌词

    [00:00.20]蓝莲花   
    [00:00.80]没有什么能够阻挡   
    [00:06.53]你对自由地向往   
    [00:11.59]天马行空的生涯  
    [00:16.53]你的心了无牵挂   
    [02:11.27][01:50.22][00:21.95]穿过幽暗地岁月   
    [02:16.51][01:55.46][00:26.83]也曾感到彷徨   
    [02:21.81][02:00.60][00:32.30]当你低头地瞬间  
    [02:26.79][02:05.72][00:37.16]才发觉脚下的路   
    [02:32.17][00:42.69]心中那自由地世界  
    [02:37.20][00:47.58]如此的清澈高远   
    [02:42.32][00:52.72]盛开着永不凋零   
    [02:47.83][00:57.47]蓝莲花  
    
class LyricsParser:
 def __init__(self, file_path):
     self.file_path = file_path

 def lyrics(self, time):
     dict1 = {}
     # 打开歌词文件
     with open(self.file_path, 'r', encoding='gbk') as f:
         lyri = f.read()
         list1 = lyri.split('\n')  # 按行切割
         keys_list = []
         for lyr in list1:
             list1 = lyr.split(']')
             keys = []
             value = None
             for index in list1:
                 #  判断是否是歌词(前面是否有xx:xx.xx格式的数字)
                 if len(index[1:]) == 8 and index[3] == ':' and index[-3] == '.':
                     str1 = index[1:3] + index[4:6] + index[7:9]
                     if str1.isdigit():
                         t1 = float(index[1:3])*60+float(index[4:9])
                         keys_list.append(t1)
                         keys.append(t1)
                     else:
                         value = index
                 elif index != '\n':
                     value = index
             for key in keys:
                 dict1.update({key: value})
     keys_list.sort()
     for i in range(len(keys_list)):
         if keys_list[i] >= time:
             print(dict1[keys_list[i-1]])
             break


def main():
 feil = input('输入文件路径:')
 l1 = LyricsParser(feil)
 while True:
     n = float(input('输入查看时间:'))
     l1.lyrics(n)


if __name__ == '__main__':
 main()

你可能感兴趣的:(day15-作业)