Python开发需求:实现按车轮数统计车辆的catalog()函数
实现统计指定车轮数车辆数量的catalog()函数
需求说明
- 编写
catalog()函数,统计列表中拥有指定车轮数的车辆总数 - 必须保留调用语句:
catalog = catalog(all_vehicles, 6),不可修改 - 输出格式要求:
- 调用
catalog = catalog(all_vehicles, 6)时,输出:Found 2 vehicles with 6 wheels - 调用
catalog = catalog(all_vehicles, 2)时,输出:Found 4 vehicles with 2 wheels
- 调用
原有车辆类结构及待完善代码
class Vehicles: color = '' wheels = [] def __init__(self, color, wheels): self.color = color self.wheels = wheels def filter(): pass def __str__(self): print(f"The bus is {self.color} and has {self.wheels} wheels.") bus1 = Vehicles("white", 6) bus1.__str__() bus2 = Vehicles("blue", 6) bus2.__str__() class Car(Vehicles): speed = 0 displacement = 0 def __init__(self, color, wheels, speed, displacement): super().__init__(color, wheels) self.color = color self.wheels = wheels self.speed = speed self.displacement = displacement def __str__(self): print(f"The car is {self.color}, has {self.wheels} wheels, is traveling at {self.speed} mph and has {self.displacement} displacement.") car1 = Car("black", 4, 70, 1000) car1.__str__() car2 = Car("grey", 4, 65, 950) car2.__str__() car3 = Car("green", 4, 90, 1100) car3.__str__() class Truck(Car): cargo = 0 def __init__(self, color, wheels, speed, displacement, cargo): super().__init__(color, wheels, speed, displacement) self.color = color self.wheels = wheels self.speed = speed self.displacement = displacement self.cargo = cargo def __str__(self): print(f"The truck is {self.color}, has {self.wheels} wheels, is traveling at {self.speed} mph, has {self.displacement} displacement and is carrying {self.cargo} weight as cargo.") truck1 = Truck("grey", 4, 40, 900, 1.525) truck1.__str__() truck2 = Truck("white", 4, 45, 920, 2.253) truck2.__str__() class Bike(Vehicles): gears = "" def __init__(self, color, wheels, gears): super().__init__(color, wheels) self.color = color self.wheels = wheels self.gears = gears def __str__(self): print(f"The bike is {self.color}, has {self.wheels} wheels and has {self.gears} changes.") bike1 = Bike("orange", 2, 12) bike1.__str__() bike2 = Bike("black", 2, 10) bike2.__str__() class Motorbike(Bike): speed = 0 displacement = 0 model = "" def __init__(self, color, wheels, gears, speed, displacement, model): super().__init__(color, wheels, gears) self.color = color self.wheels = wheels self.gears = gears self.speed = speed self.displacement = displacement self.model = model def __str__(self): print(f"The motorbike is {self.color}, has {self.wheels} wheels, has {self.gears} changes, is traveling at {self.speed} mph, has {self.displacement} displacement and is a {self.model} motorbike.") motorbike1 = Motorbike("blue", 2, 5, 120, 600, "road") motorbike1.__str__() motorbike2 = Motorbike("black", 2, 7, 220, 1100, "race") motorbike2.__str__() all_vehicles = [bus1, bus2, car1, car2, car3, truck1, truck2, bike1, bike2, motorbike1, motorbike2] def catalog(the_list, wheels): print(f"Founded {Vehicles} with {Vehicles.wheels} wheels.") catalog = catalog(all_vehicles, 6)
修正后的catalog()函数
def catalog(the_list, wheels): count = 0 # 遍历车辆列表,统计符合条件的数量 for vehicle in the_list: if vehicle.wheels == wheels: count += 1 # 按要求格式输出 print(f"Found {count} vehicles with {wheels} wheels") # 返回统计结果,满足调用语句的赋值需求 return count catalog = catalog(all_vehicles, 6)
说明
- 遍历传入的车辆列表,检查每个车辆对象的
wheels属性是否等于指定值 - 统计符合条件的车辆总数后,按照要求的字符串格式输出
- 函数返回统计结果,适配调用语句的赋值逻辑
内容的提问来源于stack exchange,提问作者Raphael Silva
相关产品推荐
相关产品推荐

