- 建立一個(gè)汽車類Auto将鸵,包括輪胎個(gè)數(shù)勉盅,汽車顏色,車身重量顶掉,速度等屬性草娜,并通過不同的構(gòu)造方法創(chuàng)建實(shí)例。至少要求 汽車能夠加速 減速 停車痒筒。 再定義一個(gè)小汽車類CarAuto 繼承Auto 并添加空調(diào)宰闰、CD屬性,并且重新實(shí)現(xiàn)方法覆蓋加速簿透、減速的方法
import time
class Auto:
def __init__(self, number, color, weigth, rate):
if number == 4 or number == 6 or number ==10:
self._number = number
else:
raise ValueError
self._color = color
if (isinstance(weigth, int) or isinstance(weigth, float)) and weigth > 0:
self._weight = weigth
else:
raise ValueError
if rate >= 0:
self._rate = rate
else:
raise ValueError
@property
def number(self):
return self._number
@number.setter
def number(self, value):
if value == 4 or value == 6 or value ==10:
self._number = value
else:
raise ValueError
@property
def weigth(self):
return self._weigth
@number.setter
def weigth(self, value):
if (isinstance(value, int) or isinstance(value, float)) and value > 0:
self._weigth = value
else:
raise ValueError
@property
def rate(self):
return self._rate
@number.setter
def rate(self, value):
if value >= 0:
self._rate = value
else:
raise ValueError
def slow_down(self):
speed = 15
while True:
time.sleep(1)
if self._rate < 60:
print('當(dāng)前速度為{}移袍,速度不夠請(qǐng)加速'.format(self._rate))
break
if speed == 0:
print('汽車正在以每秒{}m的速度行駛'.format(self._rate))
break
self._rate -= speed
print('汽車正在以每秒{}m的速度減速'.format(speed))
print('當(dāng)前速度為{}'.format(self._rate))
speed -=1
if speed <= 0:
speed = 0
def to_speed_up(self):
speed = 1
while True:
time.sleep(1)
if self._rate > 120:
print('當(dāng)前速度為{},已超速老充,請(qǐng)減速'.format(self._rate))
break
self._rate += speed
print('汽車正在以每秒{}m的速度加速'.format(speed))
print('當(dāng)前速度為{}'.format(self._rate))
speed +=3
def stop(self):
speed = 22
while True:
time.sleep(1)
self._rate -= speed
if self._rate <= 0:
self._rate = 0
print('當(dāng)前速度為{}汽車已經(jīng)停車'.format(self._rate))
break
print('汽車正在以每秒{}m的速度減速'.format(speed))
print('當(dāng)前速度為{}'.format(self._rate))
speed -= 2
class CarAuto (Auto):
def __init__(self, number, color, weigth, rate, air_conditioner=None, music=None):
super().__init__(number, color, weigth, rate)
self.air_conditioner = air_conditioner
self.music = music
def slow_down(self):
speed = 15
while True:
time.sleep(1)
if self._rate < 60:
print('當(dāng)前速度為{}葡盗,速度不夠請(qǐng)加速'.format(self._rate))
break
if speed == 0:
print('汽車正在以每秒{}m的速度行駛'.format(self._rate))
break
self._rate -= speed
print('汽車正在以每秒{}m的速度減速'.format(speed))
print('當(dāng)前速度為{}'.format(self._rate))
speed -=1
if speed <= 0:
speed = 0
def to_speed_up(self):
speed = 4
while True:
time.sleep(1)
if self._rate >= 120:
print('當(dāng)前速度為{},已超速啡浊,請(qǐng)減速'.format(self._rate))
break
self._rate += speed
print('汽車正在以每秒{}m的速度加速'.format(speed))
print('當(dāng)前速度為{}'.format(self._rate))
speed +=2
def main(self):
with open('./car.txt', 'r') as car_file:
page = car_file.read()
while True:
print(page)
case = int(input('請(qǐng)選擇功能'))
if case == 1:
self.to_speed_up()
elif case == 2:
self.slow_down()
elif case == 3:
self.stop()
elif case == 4:
quirt()
else:
print('輸入有誤')
main(self)
def quirt():
exit()
if __name__ == '__main__':
car = CarAuto(4, '黑色', 2000, 80)
main(car)
- 創(chuàng)建一個(gè)Person類觅够,添加一個(gè)類字段用來統(tǒng)計(jì)Perosn類的對(duì)象的個(gè)數(shù)
class Person(object):
"""docstring for Person"""
people = 0
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
Person.people += 1
p1 = Person('小陳', 18, '女')
p2 = Person('小李', 19, '男')
p3 = Person('小王', 20, '男')
print(Person.people)
-
創(chuàng)建一個(gè)動(dòng)物類,擁有屬性:性別巷嚣、年齡喘先、顏色、類型 廷粒,
要求打印這個(gè)類的對(duì)象的時(shí)候以'/XXX的對(duì)象: 性別-? 年齡-? 顏色-? 類型-?/' 的形式來打印
class Animal():
"""docstring for Animal"""
def __init__(self, mold, age, color, sex):
self.sex = sex
self.age = age
self.color = color
self.mold = mold
def show(self):
print('{}的對(duì)象:類型為{}, 年齡為{}苹祟,顏色為{},性別為{}'.format(Animal.__name__, self.mold, self.age, self.color, self.sex))
panda = Animal('熊貓', 3, '黑白', '公')
panda.show()
- 寫一個(gè)圓類评雌, 擁有屬性半徑树枫、面積和周長;要求獲取面積和周長的時(shí)候的時(shí)候可以根據(jù)半徑的值把對(duì)應(yīng)的值取到景东。但是給面積和周長賦值的時(shí)候砂轻,程序直接崩潰,并且提示改屬性不能賦值
from math import pi
class CircleValueError(Exception):
"""docstring for CircleValueError"""
def __str__(self):
return '該屬性不能被賦值'
class Circle(object):
"""docstring for Circle"""
def __init__(self, radius, area=None, permeter=None):
if (isinstance(radius, int) or isinstance(radius, float)) and radius > 0:
self._radius = radius
else:
raise ValueError
self._area = area
self._permeter = permeter
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
if (isinstance(value, int) or isinstance(value, float)) and value > 0:
self._radius = value
else:
raise ValueError
@property
def area(self):
return pi * self._radius ** 2
@area.setter
def area(self, value):
raise CircleValueError
@property
def permeter(self):
return 2 * pi * self._radius
@permeter.setter
def permeter(self, value):
raise CircleValueError
circle = Circle(3)
print(circle.area)
print(circle.permeter)
circle.area = 10
- 寫一個(gè)撲克類斤吐, 要求擁有發(fā)牌和洗牌的功能(具體的屬性和其他功能自己根據(jù)實(shí)際情況發(fā)揮)
import random
import time
class Poker(object):
"""docstring for Poker"""
def __init__(self):
self.design_color = ['?', '?', '?', '?']
self.points = ['2', '3', '4','5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
self.king = ['大王', '小王']
self.deck = [i+j for i in self.points for j in self.design_color]
self.deck.extend(self.king)
self.player_1 = []
self.player_2 = []
self.player_3 = []
self.card = []
def shuffle(self):
print('正在洗牌中......')
time.sleep(5)
random.shuffle(self.deck)
def deal(self):
self.player_1.extend(self.deck[0:2])
self.player_2.extend(self.deck[2:4])
self.player_3.extend(self.deck[4:6])
self.card.extend(self.deck[51:])
for i in range(5):
self.player_1.extend(self.deck[6 + i * 9: 6 + i * 9 + 3])
self.player_2.extend(self.deck[9 + i * 9: 9 + i * 9 + 3])
self.player_3.extend(self.deck[12 + i * 9: 12 + i * 9 + 3])
num = random.randint(1, 14)
if num % 3 == 1:
# self.player_1.extend(self.card)
print('恭喜player_1成為地主')
if num % 3 == 2:
self.player_2.extend(self.card)
print('恭喜player_2成為地主')
if num % 3 == 0:
self.player_3.extend(self.card)
print('恭喜player_3成為地主')
time.sleep(2)
print('正在發(fā)牌中......')
time.sleep(5)
print(self.player_1)
print(self.player_2)
print(self.player_3)
p = Poker()
p.shuffle()
p.deal()
-
(嘗試)寫一個(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]你的心了無牽掛 [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)蓮花