建立一个汽车类 包括轮胎个数 汽车颜色 车身重量等属性。并通过不同的构造方法创建实例。 至少要求 汽车能够加速 减速 停车。 要求:命名规范 代码体现层次 有友好的操作提示。
时间: 2023-06-18 18:07:39 浏览: 73
好的,以下是我的代码实现:
```python
class Car:
def __init__(self, tire_num, color, weight):
self.tire_num = tire_num
self.color = color
self.weight = weight
self.speed = 0
def accelerate(self, speed):
self.speed += speed
print("当前速度为:{}km/h".format(self.speed))
def brake(self, speed):
self.speed -= speed
if self.speed < 0:
self.speed = 0
print("当前速度为:{}km/h".format(self.speed))
def stop(self):
self.speed = 0
print("车已停止")
if __name__ == '__main__':
tire_num = input("请输入轮胎个数:")
color = input("请输入汽车颜色:")
weight = input("请输入车身重量:")
car = Car(tire_num, color, weight)
while True:
print("请选择您要进行的操作:")
print("1. 加速")
print("2. 减速")
print("3. 停车")
print("4. 退出")
choice = input("请输入您的选择:")
if choice == '1':
speed = int(input("请输入加速的速度:"))
car.accelerate(speed)
elif choice == '2':
speed = int(input("请输入减速的速度:"))
car.brake(speed)
elif choice == '3':
car.stop()
elif choice == '4':
break
else:
print("输入有误,请重新输入!")
```
使用时,用户需要输入汽车的轮胎个数、颜色和重量,然后就可以通过不同的操作选择加速、减速或停车。这个类使用了友好的操作提示,并且代码的层次感也比较明显。