-
建立一個(gè)汽車類Auto春感,包括輪胎個(gè)數(shù)润梯,汽車顏色,車身重量甥厦,速度等屬性,并通過不同的構(gòu)造方法創(chuàng)建實(shí)例寇钉。至少要求 汽車能夠加速 減速 停車刀疙。 再定義一個(gè)小汽車類CarAuto 繼承Auto 并添加空調(diào)、CD屬性扫倡,并且重新實(shí)現(xiàn)方法覆蓋加速谦秧、減速的方法
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('汽車沒啟動(dòng)') def stop(self): if self.speed >= 1: while True: self.speed -= 10 if self.speed <= 0: break print('靠邊停車') else: print('汽車已經(jīng)停了') 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('汽車已經(jīng)停車') else: print('汽車還未啟動(dòng)') 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()
?
-
創(chuàng)建一個(gè)Person類疚鲤,添加一個(gè)類字段用來(lái)統(tǒng)計(jì)Perosn類的對(duì)象的個(gè)數(shù)
class Person: num = 0 def __init__(self): Person.num += 1 p1 = Person() p2 = Person() print(Person.num)
?
-
創(chuàng)建一個(gè)動(dòng)物類,擁有屬性:性別缘挑、年齡集歇、顏色、類型 语淘,
要求打印這個(gè)類的對(duì)象的時(shí)候以'/XXX的對(duì)象: 性別-? 年齡-? 顏色-? 類型-?/' 的形式來(lái)打印
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 '{}的對(duì)象:性別-{} 年齡-{} 顏色-{} 品種-{}'.format(self.__class__.__name__, self.gender, self.age, self.color, self.bread)
?
-
寫一個(gè)圓類诲宇, 擁有屬性半徑、面積和周長(zhǎng)惶翻;要求獲取面積和周長(zhǎng)的時(shí)候的時(shí)候可以根據(jù)半徑的值把對(duì)應(yīng)的值取到姑蓝。但是給面積和周長(zhǎng)賦值的時(shí)候,程序直接崩潰吕粗,并且提示改屬性不能賦值
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
?
-
寫一個(gè)撲克類纺荧, 要求擁有發(fā)牌和洗牌的功能(具體的屬性和其他功能自己根據(jù)實(shí)際情況發(fā)揮)
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
?
-
(嘗試)寫一個(gè)類,其功能是:1.解析指定的歌詞文件的內(nèi)容 2.按時(shí)間顯示歌詞 提示:歌詞文件的內(nèi)容一般是按下面的格式進(jìn)行存儲(chǔ)的。歌詞前面對(duì)應(yīng)的是時(shí)間宙暇,在對(duì)應(yīng)的時(shí)間點(diǎn)可以顯示對(duì)應(yīng)的歌詞
[00:00.20]藍(lán)蓮花 [00:00.80]沒有什么能夠阻擋 [00:06.53]你對(duì)自由地向往 [00:11.59]天馬行空的生涯 [00:16.53]你的心了無(wú)牽掛 [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]當(dāng)你低頭地瞬間 [02:26.79][02:05.72][00:37.16]才發(fā)覺腳下的路 [02:32.17][00:42.69]心中那自由地世界 [02:37.20][00:47.58]如此的清澈高遠(yuǎn) [02:42.32][00:52.72]盛開著永不凋零 [02:47.83][00:57.47]藍(lán)蓮花
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格式的數(shù)字)
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('輸入查看時(shí)間:'))
l1.lyrics(n)
if __name__ == '__main__':
main()