-
建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto: def __init__(self, tyre, color, weight, speed): self.lt_num = tyre self.color = color self.weight = weight self.speed = speed def speed_change(self, numbers: int): self.speed += numbers def park(self): self.speed = 0 class CarAuto(Auto): def __init__(self, air_conditioner, cd, tyre, color, weight, speed): super().__init__(tyre, color, weight, speed) self.air_conditioner = air_conditioner self.cd = cd def speed_change(self, numbers: int): self.speed += numbers * 2
-
创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person: count = 0 def __init__(self): Person.count += 1
-
创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以’/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/’ 的形式来打印
class Animal: def __init__(self, gender, age, color, types): self.gender = gender self.age = age self.color = color self.types = types def __repr__(self): return f'{self.__class__.__name__}的对象:性别:{self.gender},年龄:{self.age},颜色:{self.color},类型:{self.types}'
-
(不写)写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
-
写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random class Poker: color = ['♠', '♥', '♣', '♦'] numb = ['2', 'A', 'K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3'] poker = ['joker', 'JOKER'] for x in numb: for y in color: poker.append(y + x) def __init__(self): self.player1 = [] self.player2 = [] self.player3 = [] self.base = [] def shuffle(self): self.base = random.choices(Poker.poker, k=3) self.player1 = random.choices(list(set(Poker.poker) - set(self.base)), k=17) self.player2 = random.choices(list(set(Poker.poker) - set(self.base) - set(self.player1)), k=17) self.player3 = list(set(Poker.poker) - set(self.base) - set(self.player1) - set(self.player2)) self.base.sort(key=lambda x: Poker.poker.index(x)) self.player1.sort(key=lambda x: Poker.poker.index(x)) self.player2.sort(key=lambda x: Poker.poker.index(x)) self.player3.sort(key=lambda x: Poker.poker.index(x)) def deal(self): print(f'底牌:({self.base}),玩家1:({self.player1}),玩家2:({self.player2}),玩家3:({self.player3})') game = Poker() game.shuffle() game.deal()
-
(尝试)写一个类,其功能是: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]蓝莲花 import re class MusicWord: timeKeys = re.compile(r'\[\d\d:\d\d\.\d\d\]') def __init__(self, data: str): self.times = MusicWord.timeKeys.findall(data) self.words = [] for x in self.times: wordKey = re.compile('\[' + x[1:-1] + '\]' + r'[^\u4e00-\u9fa5]*[\u4e00-\u9fa5]+') word = re.findall(wordKey, data) words = re.findall('[\u4e00-\u9fa5]+', word[0]) self.words.append(words) self.time = [] for x in self.times: num = float(x[1:3]) + float(x[4:-1]) self.time.append(num) def See(self): a = map(lambda x, y: [x, y], self.time, self.words) a = list(a) a.sort(key=lambda x: x[0]) print(a) a = MusicWord(""" [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]蓝莲花 """) a.See()
day18作业(1)
最新推荐文章于 2023-11-11 10:46:35 发布