Home > Back-end >  Print a list of specific parameters from a class through a list?
Print a list of specific parameters from a class through a list?

Time:04-13

I think I've worded that question right, basically I need to display a list of all car makes available currently. Here is my code

class Vehicle:
    def __init__(self, registration_no, make, model, colour, selling_price, cost, branch):
        self.registration_no = registration_no
        self.make = make
        self.model = model
        self.colour = colour
        self.selling_price = selling_price
        self.cost = cost
        self.branch = branch
        
    def get_make(self):
        print(self.make)
        return self.make

    def get_model(self):
        return self.model
    def get_colour(self):
        return self.colour
    def get_regi_no(self):
        return self.registration_no
    def get_sell_price(self):
        return self.selling_price
    def get_cost(self):
        return self.cost
    def get_branch(self):
        return self.branch
    
class Car(Vehicle):
    def __init__(self, registration_no, make, model, colour, selling_price, cost, branch, doors):
        super().__init__(registration_no, make, model, colour, selling_price, cost, branch)
        self.doors = doors

    def no_of_doors(self):
        print("The", self.make, self.model, "has", self.doors, "doors.")


Car1 = Car("AT11CAR", "Audi", "R8", "Black", "500", "250", "Dundee", "3")
Car2 = Car("AT12CAR", "Ferrari", "Spider","Red", "1000", "500", "Aberdeen", "3")
Car3 = Car("AT13CAR", "Mercedes", "S-Class", "Black", "750", "325", "Troon", "5")
Car4 = Car("AT14CAR", "Rolls Royce", "Phantom", "Silver","750", "325", "Arbroath", "5")
Car5 = Car("AT15CAR", "Mitsubishi", "Warrior", "Blue", "400", "200", "Aberdeen", "5")
Car6 = Car("AT16CAR", "Ford", "Ranger", "Orange", "600", "300", "Troon", "5")
Car7 = Car("AT17CAR", "Volkswagen", "Touran", "Grey", "200", "100", "Arbroath", "5")
Car8 = Car("AT18CAR", "Honda", "CRZ", "White", "250", "125", "Dundee", "3")

car_dict = {Car1.make : Car1,
            Car2.make : Car2,
            Car3.make : Car3,
            Car4.make : Car4,
            Car5.make : Car5,
            Car6.make : Car6, 
            Car7.make : Car7,
            Car8.make : Car8}

cars = [Car1, Car2, Car3, Car4, Car5, Car6, Car7, Car8]

print(Car1.make)

for m in cars:
    print(Car1.make)

I would like it to look like

"Makes Available: " Audi Ferrari Mercedes etc. etc.

I currently can print one make for each car so Audi x8 or I can print Audi itself. How do I do this? print each unique make out when prompted.

CodePudding user response:

If I understood correctly your problem, you want to print car_dict keys:

print("Makes Available: ")
for make in car_dict.keys():
    print(make)

Edit: if you want to print all in the same line, add " " as parameter end to the prints (ie, print("Makes Available: ", end=" ") and print(make, end=" "))

CodePudding user response:

first iterate through cars and add new car make in makes list. finally print it.

cars = [Car1, Car2, Car3, Car4, Car5, Car6, Car7, Car8]
makes = []
for car in cars:
    if car.make not in makes: makes.append(car.make)
print('Makes Available:',' '.join(makes))
  • Related