Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function go trought list

I need to create a function called catalog() to find out how many vehicles there are with the amount of wheels I choose. The only thing I can’t change is the call "catalog = catalog(all_vehicles, 6)"

For example:

If I do like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

catalog = catalog(all_vehicles, 6)

It should come out like this:

"Found 2 vehicles with 6 wheels"

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

>Solution :

When you reassign catalog at the very end, you’re overriding the definition of the function. So, subsequent invocation will fail. Also, you need some kind of loop in the catalog function with an accumulator. There are multiple ways to do this in Python, but I’ll present the basic way that’s readable to newcomers.

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)

This will help you move the ball down the field. Your data structures and naming are a bit wonky. Do you want a list for wheels? Or perhaps just store number? Your next steps are to clean up how you want to represent the data and then come back to tweak my proposed definition of catalog.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading