- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self,number,color,weight,speed):
self.number = number
self.color = color
self.weight = weight
self.speed = speed
def add_speed(self):
self.speed += 10
def reduce_speed(self):
self.speed -= 10
def stop_speed(self):
self.speed = 0
class CarAuto(Auto):
def __init__(self,air_conditoner,cd,number,color,weight,speed):
super().__init__(self,number,color,weight,speed)
self.air_conditoner = air_conditoner
self.cd = cd
def add_speed(self):
self.speed += 20
def reduce_speed(self):
self.speed -= 20
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
count = 0
def __init__(self):
Person.count += 1
- 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self,gender,age,color,type):
self.gender = gender
self.age = age
self.color = color
self.type = type
def __repr__(self):
return '{}的对象:性别-{}年龄-{}颜色-{}类型-{}'.format(Animal.__name__,self.gender,self.age,self.color,self.type)
animal1 = Animal('公','5','white','dog')
print(animal1)
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
from math import pi
class ChangeError(Exception):
def __str__(self):
return '该属性不能赋值'
class Circle:
pi = pi
def __init__(self,radius):
self.radius = radius
self._area = pi * radius ** 2
self._perimeter = 2 * radius * pi
@property
def area(self):
return self._area
@area.setter
def area(self,value):
raise ChangeError
@property
def perimeter(self):
return self._perimeter
@perimeter.setter
def perimeter(self,value):
raise ChangeError
- 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import colorama
print(colorama.Fore.MAGENTA)
import random
class Poker:
poker_ = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'J', 'K', 'Q']
color_ = [chr(9824),chr(9827),chr(9829),chr(9830)]
card = ['king', 'kinglet']
players = []
def __init__(self, n: int):
self.n = n
self.players = Poker.players
def shuffle(self):
for color in self.color_:
for poker in self.poker_:
self.card.append((color, poker))
random.shuffle(self.card)
def deal(self):
if not self.players:
for i in range(self.n):
self.players.append([])
else:
for i in range(self.n):
self.players[i].clear()
for i in range(self.n):
for j in range(3):
self.players[i].append(self.card.pop())
if len(self.card) < 3*self.n:
self.shuffle()
- (尝试)写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式进行存储的。歌词前面对应的是时间,在对应的时间点可以显示对应的歌词
import re
list1 = []
with open('song_word.txt','r',encoding='utf-8')as f:
str1 = f.readline()
if str1.split(']')[-1] != '':
while str1:
str1 = re.split(r'\[|\]|\n', str1.replace(':', ''))
try:
while True:
str1.remove('')
except:
pass
list1.append(str1)
str1 = f.readline()
for value in list1[:]:
if len(value) > 2:
for index in range(len(value)-1):
list1.append([value[index],value[-1]])
list1.remove(value)
list1.sort(key=lambda x:float(x[0]))
for index in range(len(list1)-1):
list1[index].insert(1,list1[index+1][0])
while True:
time1 = input('请输入你的时间:')
time1 = float(time1.replace(':',''))
print(time1)
if time1 > float(list1[-1][0]):
print(list1[-1][1])
else:
for value in list1[:-1]:
if float(value[0]) < time1 < float(value[1]):
print(value[2])