- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self, tyre, color, weight, speed):
self.tyre = tyre
self.color = color
self.weight = weight
self.speed = speed
def up_speed(self):
while self.speed <= 80:
self.speed += 1
if self.speed == 120:
break
def down_speed(self):
while self.speed >= 120:
self.speed -= 1
if self.speed == 60:
break
def stop(self):
while True:
self.speed -= 1
if self.speed == 0:
break
class CarAuto(Auto):
def __init__(self, air_condition, cd_player):
super().__init__()
self.air_condition = air_condition
self.cd_player = cd_player
def up_speed(self):
while self.speed <= 80:
self.speed += 1
if self.speed == 120:
break
def down_speed(self):
while self.speed >= 120:
self.speed -= 1
if self.speed == 60:
break
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
count = 0
def __init__(self, name, age, gender, grade):
self.name = name
self.age = age
self.gender = gender
self.grade = grade
Person.count += 1
print(Person.count)
- 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animals:
def __init__(self, gender, age, color, breed):
self.gender = gender
self.age = age
self.color = color
self.breed = breed
def out(self,name):
print('\'/%s的对象:性别-%s 年龄-%s 颜色-%s 类型-%s?/\'' % (name, self.gender, self.age, self.color, self.breed))
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class WriteError(Exception):
def __str__(self):
return '该属性不能赋值!'
class Circle:
pi = 3.14
def __init__(self, r):
self.r = r
self._area1 = 0
self._perimeter1 = 0
@property
def area(self):
self.area1 = self.pi * self.r ** 2
return self.area1
@area.setter
def area(self, num):
raise WriteError
@property
def perimeter(self):
self.perimeter1 = self.pi * self.r * 2
return self.perimeter1
@perimeter.setter
def perimeter(self, num1):
raise WriteError
c = Circle(3)
print(c.area) #28.26
print(c.perimeter) #18.84
c.perimeter = 13 # raise WriteError
#__main__.WriteError: 该属性不能赋值!
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random
class Poke:
pokes = []
def __init__(self, poke_flower=['♠', '♥', '♣', '♦'],
poke_numbers=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'],
poke_b='大王',
poke_s='小王',
with_brand='配牌'
):
self.poke_flower = poke_flower
self.poke_numbers = poke_numbers
self.poke_b = poke_b
self.poke_s = poke_s
self.with_brand = with_brand
def shuffle(self):
self.pokes = []
# self.pokes.append(self.with_brand)
self.pokes.append(self.poke_b)
self.pokes.append(self.poke_s)
for flower in self.poke_flower:
for numbers in self.poke_numbers:
self.pokes.append(flower + numbers)
random.shuffle(self.pokes)
def deal(self, player):
self.shuffle()
if player == 3:
num = input('是否需要配牌y/n:')
if num == 'y':
self.pokes.append(self.with_brand)
random.shuffle(self.pokes)
else:
pass
player1 = self.pokes[:17]
player2 = self.pokes[17:34]
player3 = self.pokes[34:51]
dizhu = self.pokes[51:]
print(dizhu)
print(player1)
print(player2)
print(player3)
if player == 4:
self.pokes.remove(self.poke_s)
self.pokes.remove(self.poke_b)
# self.pokes.remove(self.with_brand)
player1 = self.pokes[:13]
player2 = self.pokes[13:26]
player3 = self.pokes[26:39]
player4 = self.pokes[39:52]
print(player4)
print(player1)
print(player2)
print(player3)
else:
print('不支持该玩法')
def landlords(self, player):
self.deal(player)
def run_fast(self, player):
self.deal(player)
#=================================
@unique
class PokerNum(Enum):
J = 11, 'J', 11
Q = 12, 'Q', 12
K = 13, 'K', 13
A = 1, 'A', 14
TWO = 2, '2', 15
BigJoker = 15, '大王', 17
SmallJoker = 14, '小王', 16
@unique
class PokerColor(Enum):
Club = '♣'
Diamond = '♦'
Heart = '♥'
Spade = '♠'
Space = ''
class Poker:
def __init__(self, num: PokerNum, color: PokerColor):
self.num = num
self.color = color
def __repr__(self):
if isinstance(self.num, PokerNum):
return str(self.color.value) + str(self.num.value[1])
return str(self.color.value)+str(self.num)
def __gt__(self, other):
if isinstance(self.num, PokerNum):
a = self.num.value[2]
else:
a = self.num
if isinstance(other.num, PokerNum):
b = other.num.value[2]
else:
b = other.num
return a > b
class Game:
# 斗地主
def __init__(self):
pokers = []
colors = [PokerColor.Club, PokerColor.Diamond, PokerColor.Heart, PokerColor.Spade]
nums = [PokerNum.A, PokerNum.TWO, 3, 4, 5, 6, 7, 8, 9, 10, PokerNum.J, PokerNum.Q, PokerNum.K]
for num in nums:
for color in colors:
p = Poker(num, color)
pokers.append(p)
pokers.extend([Poker(PokerNum.SmallJoker, PokerColor.Space), Poker(PokerNum.BigJoker, PokerColor.Space)])
self.pokers = pokers
self.pokers_iter = iter(self.pokers)
# 洗牌
def shuffling(self):
shuffle(self.pokers)
self.pokers_iter = iter(self.pokers)
# 斗地主发牌方式
def deal1(self):
ps1 = []
ps2 = []
ps3 = []
for _ in range(17):
ps1.append(next(self.pokers_iter))
ps2.append(next(self.pokers_iter))
ps3.append(next(self.pokers_iter))
ps1.sort(reverse=True)
ps2.sort(reverse=True)
ps3.sort(reverse=True)
return ps1, ps2, ps3, list(self.pokers_iter)
game1 = Game()
game1.shuffling()
p1, p2, p3, di = game1.deal1()
print(p1)
print(p2)
print(p3)
print('底牌:', di)
- (尝试)写一个类,其功能是: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 Lyric:
def __init__(self, time, word):
value = float(time[1:3])*60 + float(time[4:])
self.time = value
self.word = word
def __repr__(self):
return '<'+str(self.time) + ':' + self.word+'>'
def __gt__(self, other):
return self.time > other.time
class LyricAnalysis:
def __init__(self, name):
# 歌名
self.name = name
self.__all_lyrics = []
def get_word(self, time):
# =======解析歌词文件======
if not self.__all_lyrics:
print('解析歌词')
with open('files/'+self.name) as f:
while True:
line = f.readline()
if not line:
break
lines = line.split(']')
word = lines[-1]
for t in lines[:-1]:
lyric = Lyric(t, word)
self.__all_lyrics.append(lyric)
# 排序
self.__all_lyrics.sort(reverse=True)
# ==========获取歌词==========
for lyric in self.__all_lyrics:
if lyric.time <= time:
return lyric.word
ly = LyricAnalysis('蓝莲花')
print('===:',ly.get_word(123))
print('===:',ly.get_word(10))
print('===:',ly.get_word(89))
ly2 = LyricAnalysis('一首简单的歌')
print('!!!:', ly2.get_word(30))
print('!!!:', ly2.get_word(90))