Reputation: 29
Part of my assignment is to export to text if the user selects that option. I seem to have everything right except that one part. Can anyone suggest where I am going wrong? Do i need to add something somewhere else in the code for this to work properly? I am getting error that automobilelist
is not defined when I select Option 6.
class Automobile:
# constructor, it takes parameters make, model, year and mileage
def __init__(self,aMake,aModel,aColor,aYear,aMileage):
# assign passed values to instance variables
self.setMake(aMake) # string
self.setModel(aModel) # string
self.setColor(aColor) # string
self.setYear(aYear) # int
self.setMileage(aMileage) # int
# setter of make
# it takes 1 parameter make
# it doesn't returns anything
def setMake(self, aMake):
self.__make = aMake
# getter of make
# it doesn't take any parameters
# it returns make
def getMake(self):
return self.__make
# setter of model
# it takes 1 parameter model
# it doesn't returns anything
def setModel(self, aModel):
self.__model = aModel
# getter of model
# it doesn't take any parameters
# it returns model
def getModel(self):
return self.__model
# setter of color
# it takes 1 parameter color
# it doesn't returns anything
def setColor(self, aColor):
self.__color = aColor
return True
# getter of color
# it doesn't take any parameters
# it returns color
def getColor(self):
return self.__color
# setter of year
# it takes 1 parameter year
# it doesn't returns anything
def setYear(self, aYear):
self.__year = aYear
# getter of year
# it doesn't take any parameters
# it returns year
def getYear(self):
return self.__year
# setter of mileage
# it takes 1 parameter year
# it doesn't returns anything
def setMileage(self, aMileage):
self.__mileage = aMileage
# getter of mileage
# it doesn't take any parameters
# it returns mileage
def getMileage(self):
return self.__mileage
# string representation of the instance variables
# it doesn't take any parameters
# it returns string form of object
def __str__(self):
return "{}, {}, {}, {}, {}".format(self.getMake(),self.getModel(),self.getColor(),self.getYear(),self.getMileage())
class Automobiledealership:
# constructor
# it initializes the empty automobile list
def __init__(self):
self.__automobilelist = []
# adds automobile to dealership
# it takes automobile class object as parameter
# it doesn't returns anything
def addAutomobile(self, automobile):
self.__automobilelist.append(automobile)
# removes automobile from dealership
# it takes index of automobile in the list as parameter
# it doesn't returns anything
def removeAutomobile(self, index):
del self.__automobilelist[index-1]
# returns automobile from dealership
# it takes index of automobile in the list as parameter
# it returns automobile object located at that index
def getAutomobile(self, index):
return self.__automobilelist[index-1]
# prints the list of automobiles
# it doesn't take any parameters
# it doesn't returns anything
def printAutomobileList(self):
if len(self.__automobilelist)==0:
print("No automobiles in dealership")
else:
for i in range(len(self.__automobilelist)):
print("{}. {}".format(i+1,self.__automobilelist[i]))
# add some automobiles to the dealership for Sample Inventory
def addSampleAutomobiles(automobiles):
automobiles.addAutomobile(Automobile("Ford", "Focus", "Red", 2019, 8000))
automobiles.addAutomobile(Automobile("Toyota","Corolla","White",2007,65000))
automobiles.addAutomobile(Automobile("Ford","Endura","Black",2019,5000))
automobiles.addAutomobile(Automobile("Audio","A1","White",2016,23000))
automobiles.addAutomobile(Automobile("Toyota", "Camry", "Red", 2000, 25000))
automobiles.addAutomobile(Automobile("Ford","Everest","Black",2017,16000))
automobiles.addAutomobile(Automobile("Mazda","MX5","Red",2015,33000))
automobiles.addAutomobile(Automobile("Ford","Escape","Grey",2018,15000))
automobiles.addAutomobile(Automobile("Toyota", "Prius", "Red", 2010, 35000))
def addAutomobile(automobiles):
make = input("Please enter Make >> ")
model = input("Please enter Model >> ")
color = input("Please enter Color >> ")
year = int(input("Please enter Year >> "))
mileage = int(input("Please enter Mileage >> "))
automobiles.addAutomobile(Automobile(make,model,color,year,mileage))
def removeAutomobile(automobiles):
index = int(input("Please enter number of the automobile to be removed >> "))
automobiles.removeAutomobile(index)
def updateAutomobile(automobiles):
index = int(input("Please enter number of the automobile to be updated >> "))
automobile = automobiles.getAutomobile(index)
make = input("Please enter Make (enter 0 to skip) >> ")
model = input("Please enter Model (enter 0 to skip) >> ")
color = input("Please enter Color (enter 0 to skip) >> ")
year = int(input("Please enter Year (enter 0 to skip) >> "))
mileage = int(input("Please enter Mileage (enter -1 to skip) >> "))
if make!='0':
automobile.setMake(make)
print("Automobile make is updated")
if model!='0':
automobile.setModel(model)
print("Automobile model is updated")
if color!='0':
automobile.setColor(color)
print("Automobile color is updated")
if year!=0:
automobile.setYear(year)
print("Automobile year is updated")
if mileage!=-1:
automobile.setMileage(mileage)
print("Automobile mileage is updated")
# create automobile dealership object
automobiles = Automobiledealership()
choice = -1
# start loop
while choice!=0:
print()
print("1. Add sample automobiles to dealership")
print("2. Display automobiles in the dealership")
print("3. Add automobile to dealership")
print("4. Remove automobile from dealership")
print("5. Update automobile at dealership")
print("6. Export automobile Inventory")
print("0. Exit program")
choice = int(input("Your choice >> "))
print()
if choice==1:
addSampleAutomobiles(automobiles)
elif choice==2:
automobiles.printAutomobileList()
elif choice==3:
addAutomobile(automobiles)
elif choice == 4:
removeAutomobile(automobiles)
elif choice == 5:
updateAutomobile(automobiles)
elif choice == 6:
f = open('AutomobileList.txt', 'w')
f.write(str(automobilelist))
f.close()
Upvotes: 0
Views: 195
Reputation: 118
You get this error because automobilelist is really not defined outside the scope of the Automobiledealership class.
If you want to get field from a class, you should call <InstanceName>.<FieldName>, which in your case is automobiles.__automobilelist
.
BUT in your case it still won't work. When you prefixed __automobilelist with double underscore you defined it as a private member of Automobiledealership class, so you should also create a getter function for the automobilelist (as you did with getAutomobile)
A simple getter function you can use n your class:
def getAutoMobileList(self):
return self.__automobilelist
and then call automobiles.getAutoMobileList()
.
You called str(automobilelist)
, which as mentioned above should be str(automobiles.getAutoMobileList())
, which in turn can be translated to automobiles.__str__()
. This means that you can define an __str__
function instead of the getter function mentioned above.
So for a newline separated values you can define:
def __str__(self):
return '\n'.join(str(i) for i in self.__automobilelist)
and then just call str(automobiles)
.
Upvotes: 1
Reputation: 16
you can try creating a getter function in your class.
def getAutoMobileList(self):
return self.__automobilelist
Upvotes: 0