python 函数遍历列表

oyjwcjzk  于 2023-01-16  发布在  Python
关注(0)|答案(2)|浏览(113)

我需要创建一个名为catalog()的函数来找出有多少辆车具有我选择的车轮数量,唯一不能更改的是调用"catalog = catalog(all_vehicles,6)"
例如:
如果我真的喜欢这个
目录=目录(所有车辆,6)
应该是这样的:
"发现2辆6个轮子的车"
或目录=目录(所有车辆,2)
"发现4辆2个轮子的车"

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)
mkshixfv

mkshixfv1#

当你在最后重新赋值catalog时,你覆盖了函数的定义。因此,随后的调用将会失败。另外,你需要在catalog函数中使用某种带有累加器的循环。在Python中有多种方法可以做到这一点,但我将介绍初学者可读的基本方法。

def catalog(the_list, wheels):
  num = 0
  for vehicle in the_list:
    if vehicle.wheels == wheels:
      num += 1

  print(f"Found {num} vehicles with {wheels} wheels.")

catalog(all_vehicles, 6)
catalog(all_vehicles, 2)

这将帮助您将球移向球场。您的数据结构和命名有点不稳定。您想要一个轮子的列表吗?或者可能只是商店编号?您的下一步是清理您想要如何表示数据,然后再回来调整我提出的catalog的定义。

nhhxz33t

nhhxz33t2#

假设在对象中初始化它的方式,Vechicles.wheelsint

def catalog(the_list, wheels):   
    found = 0
    for veh in the_list:
        if veh.wheels == wheels:
            found += 1
    print(f"Found {found} with {wheels} wheels.")

相关问题