I'm working on a program for a final in a class and I'm having some troubles with updating the created inventory. I've been able to get it to the point where I can do everything else in the code, but when I go to run the update section, nothing updates. I'd be really grateful for any help with this. All of my code is included below
class Automobile:
def __init__(self, make, model, year, color, mileage=0):
self._make = make
self._model = model
self._year = year
self._color = color
self._mileage = mileage
@classmethod
def make_vehicle(cls):
make = input('Enter vehicle make: ')
model = input('Enter vehicle model: ')
while True:
year = input('Enter vehicle year: ')
try:
year = int(year)
break
except ValueError:
print("Year must be a number", file=sys.stderr)
color = input('Enter vehicle color: ')
while True:
mileage = input('Enter vehicle mileage: ')
try:
mileage = int(mileage)
break
except ValueError:
print("Mileage must be an integer", file=sys.stderr)
return cls(make, model, year, color, mileage)
def add_Vehicle(self):
vehicle = Automobile.make_vehicle()
self.vehicles.append(vehicle)
def __str__(self):
return '\t'.join(str(x) for x in [self._make, self._model, self._year, self._color, self._mileage])
class Inventory:
def __init__(self):
self.vehicles = []
def add_vehicle(self):
vehicle = Automobile.make_vehicle()
self.vehicles.append(vehicle)
def viewInventory(self):
print('\t'.join(['','Make', 'Model','Year', 'Color', 'Mileage']))
for idx, vehicle in enumerate(self.vehicles) :
print(idx + 1, end='\t')
print(vehicle)
print("This is the Vehicle Inventory Program.")
inventory = Inventory()
while True:
print ("""
1.Add a Vehicle
2.Delete a Vehicle
3.View Inventory
4.Update Inventory
5.Export Inventory
6.Quit
""")
ans=input("What would you like to do? ")
if ans=="1":
#add a vehicle
inventory.add_vehicle()
elif ans=='2':
#delete a vehicle
if len(inventory.vehicles) < 1:
print('Sorry there are no vehicles currently in inventory')
continue
inventory.viewInventory()
item = int(input('Please enter the number associated with the vehicle to be removed: '))
if item - 1 > len(inventory.vehicles):
print('This is an invalid number')
else:
inventory.vehicles.remove(inventory.vehicles[item - 1])
print ()
print('This vehicle has been removed')
elif ans == '3':
#list all the vehicles
if len(inventory.vehicles) < 1:
print('Sorry there are no vehicles currently in inventory')
continue
inventory.viewInventory()
elif ans == '4':
#edit vehicle
if len(inventory.vehicles) < 1:
print('Sorry there are no vehicles currently in inventory')
continue
inventory.viewInventory()
item = int(input('Please enter the number associated with the vehicle to be updated: '))
if item - 1 > len(inventory.vehicles):
print('This is an invalid number')
else:
if Inventory().add_vehicle() == True :
inventory.vehicles.remove(inventory.vehicles[item - 1])
inventory.vehicles.insert(item - 1, Automobile)
print('This vehicle has been updated')
elif ans == '5':
#export inventory to file
if len(inventory.vehicles) < 1:
print('Sorry there are no vehicles currently in inventory')
continue
f = open('vehicle_inventory.txt', 'w')
f.write('\t'.join(['Make', 'Model','Year', 'Color', 'Mileage']))
f.write('\n')
for vechicle in inventory.vehicles:
f.write('%s\n' %vechicle)
f.close()
print('The vehicle inventory has been exported to a file')
elif ans == '6':
#exit the loop
print('Thank you for utilizing the Vehicle Inventory Program. Have a nice day.')
break
else:
print('invalid')
As already mentioned in the comments, the if
condition is wrong and also you are inserting the class Automobile
. What you need probably is Automobile.make_vehicle()
. So, update the code,
elif ans == '4':
#edit vehicle
if len(inventory.vehicles) < 1:
print('Sorry there are no vehicles currently in inventory')
continue
inventory.viewInventory()
item = int(input('Please enter the number associated with the vehicle to be updated: '))
if item - 1 > len(inventory.vehicles):
print('This is an invalid number')
else:
inventory.vehicles.remove(inventory.vehicles[item - 1])
inventory.vehicles.insert(item - 1, Automobile.make_vehicle())
print('This vehicle has been updated')