Home > Blockchain >  Python: Adding objects to a list using Classes
Python: Adding objects to a list using Classes

Time:01-29

I am new to Classes and hope someone can help me out. I am writing av program where a user can add cars to an existing list of cars. The code I have so far is below. I am probably doing several mistakes here, but the error message I get now is "NameError: name 'car' is not defined". I think I am using the Car Class / Vehicle Class wrong. Anybody got any ideas how I can get on track with this?

class Vehicle:
    def __init__(self, Make, Model, Year, Mileage, Price):
        self.Makename = Make
        self.Modelname = Model
        self.Yearr = Year
        self.Mileagenw = Mileage
        self.Pricenw = Price
    def getValus(self):
        return self.Makename   " "   self.Modelname  " "   self.Yearr " "   self.Mileagenw " "   self.Pricenw
    def Display(self):
        print("Invetory Unit: Car \n Make: "  self.Makename   "\n Model: "   self.Modelname  "\n Year "   self.Yearr "\n Miles "   self.Mileagenw " \n Price :"   self.Pricenw)

class Car(Vehicle):
    def __init__(self, Make, Model,Year,Mileage,Price,numofdoors):
        Vehicle.__init__(self,Make, Model,Year,Mileage,Price)
        self.numofdoorsnw = numofdoors
    def GetCar(self):
        return self.getValus()   ", "   self.numofdoorsnw
    def Display(self):
        print("Invetory Unit: Car \n Make: "  self.Makename   "\n Model: "   self.Modelname  "\n Year "   self.Yearr "\n Miles "   self.Mileagenw " \n Price :"   self.Pricenw " \n Number of doors :"   self.numofdoorsnw)

def main():
    vehicles_list = []
    vehicles_list  = [Car("Tesla", "S", "2020", "170000", "33000.0", "4")]
    vehicles_list  = [Car("Tesla", "X", "2021", "180000", "23000.0", "4")]
    
    newCar = "Y"
    while newCar == "Y":
        print('Input car data')
        Make = input('Merke: ')
        Model = input('Model: ')
        Year = input('Year: ')
        Milage = input('Milage: ')
        Price = input('Price: ')
        numofdoors = input('Number of Doors: ')
        vehicles_list  = [Car(Make, Model, Year, Milage, Price, numofdoors)]
        
        newCar = input('Add another car? (Y/N) ')          
    
    print(car.getValus())
       
main()

CodePudding user response:

class Vehicle:
    def __init__(self, Make, Model, Year, Mileage, Price):
        # are you making names unique here? if so, they are in a different namespace so don't have to be different
        self.Makename = Make
        self.Modelname = Model
        self.Yearr = Year
        self.Mileagenw = Mileage
        self.Pricenw = Price
    def getValus(self):
        return self.Makename   " "   self.Modelname  " "   self.Yearr " "   self.Mileagenw " "   self.Pricenw
    def Display(self):
        print("Invetory Unit: Car \n Make: "  self.Makename   "\n Model: "   self.Modelname  "\n Year "   self.Yearr "\n Miles "   self.Mileagenw " \n Price :"   self.Pricenw)

class Car(Vehicle):
    def __init__(self, Make, Model,Year,Mileage,Price,numofdoors):
        Vehicle.__init__(self,Make, Model,Year,Mileage,Price)
        self.numofdoorsnw = numofdoors
    def GetCar(self):
        return self.getValus()   ", "   self.numofdoorsnw
    def Display(self):
        print("Invetory Unit: Car \n Make: "  self.Makename   "\n Model: "   self.Modelname  "\n Year "   self.Yearr "\n Miles "   self.Mileagenw " \n Price :"   self.Pricenw " \n Number of doors :"   self.numofdoorsnw)

def main():
    # adding unnamed instances of the class Car to a list. Each Car object is an element of the list
    vehicles_list = []
    vehicles_list  = [Car("Tesla", "S", "2020", "170000", "33000.0", "4")]
    vehicles_list  = [Car("Tesla", "X", "2021", "180000", "23000.0", "4")]
    
    newCar = "Y"
    while newCar == "Y":
        print('Input car data')
        Make = input('Merke: ')
        Model = input('Model: ')
        Year = input('Year: ')
        Milage = input('Milage: ')
        Price = input('Price: ')
        numofdoors = input('Number of Doors: ')
        vehicles_list  = [Car(Make, Model, Year, Milage, Price, numofdoors)]
        
        newCar = input('Add another car? (Y/N) ')          
    
    print(car.getValus()) # car is undefined. print the vehicles_list instead, it contains all Car objects you have made.
       
main()

Improved version:

class Vehicle:
    def __init__(self, make, model, year, mileage, price):
        self.make = make
        self.model = model
        self.year = year
        self.mileage = mileage
        self.price = price

    def getValues(self):
        return self.make   " "   self.model   " "   self.year   " "   self.mileage   " "   self.price

    def Display(self):
        print("Invetory Unit: Car \n Make: "   self.make   "\n Model: "   self.model   "\n Year "   self.year   "\n Miles "   self.mileage   " \n Price :"   self.price)

class Car(Vehicle):
    def __init__(self, Make, Model,Year,Mileage,Price,num_doors):
        Vehicle.__init__(self,Make, Model,Year,Mileage,Price)
        self.num_doors = num_doors

    def GetCar(self):
        return self.getValues()   ", "   self.num_doors

    def Display(self):
        print("Invetory Unit: Car \n Make: "  self.make   "\n Model: "   self.model  "\n Year "   self.year   "\n Miles "   self.mileage  " \n Price :"   self.price   " \n Number of doors :"   self.num_doors)

def main():
    vehicles_list = []
    vehicles_list.append(Car("Tesla", "S", "2020", "170000", "33000.0", "4"))
    vehicles_list.append(Car("Tesla", "X", "2021", "180000", "23000.0", "4"))
    
    newCar = "Y"
    while newCar == "Y":
        print('Input car data')
        Make = input('Make: ')
        Model = input('Model: ')
        Year = input('Year: ')
        Milage = input('Milage: ')
        Price = input('Price: ')
        numofdoors = input('Number of Doors: ')

        vehicles_list.append(Car(Make, Model, Year, Milage, Price, numofdoors))
        
        newCar = input('Add another car? (Y/N) ')          
    
    for car in vehicles_list:
        print(car.GetCar())
       
main()

result:

Input car data
Make: VW
Model: Golf
Year: 2020
Milage: 10000
Price: 20000
Number of Doors: 5
Add another car? (Y/N) n
Tesla S 2020 170000 33000.0, 4
Tesla X 2021 180000 23000.0, 4
VW Golf 2020 10000 20000, 5

On the point about class namespaces, there is a great talk from Raymond Hettinger explaining those : https://www.youtube.com/watch?v=8moWQ1561FY

CodePudding user response:

On print(car.getValus()), you want to get values from an object of class Car called car but you never defined this object. You probably want to modify your main in the following way

def main():
    vehicles_list = []
    vehicles_list  = [Car("Tesla", "S", "2020", "170000", "33000.0", "4")]
    vehicles_list  = [Car("Tesla", "X", "2021", "180000", "23000.0", "4")]
    
    newCar = "Y"
    while newCar == "Y":
        print('Input car data')
        Make = input('Merke: ')
        Model = input('Model: ')
        Year = input('Year: ')
        Milage = input('Milage: ')
        Price = input('Price: ')
        numofdoors = input('Number of Doors: ')
        input_car = Car(Make, Model, Year, Milage, Price, numofdoors)
        vehicles_list  = [input_car]

        newCar = input('Add another car? (Y/N) ') 
     
    for car in veichles_list:
        print(car.getValus())
  •  Tags:  
  • Related