Python 代码中更新库存的问题

Issues with updating an inventory in Python Code

我正在为 class 中的决赛编写程序,但在更新创建的清单时遇到了一些麻烦。我已经能够做到我可以在代码中执行所有其他操作的地步,但是当我转到 运行 更新部分时,没有任何更新。如果对此有任何帮助,我将不胜感激。我的所有代码都包含在下面

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

正如评论中已经提到的,if 条件是错误的,而且您正在插入 class Automobile。您可能需要的是 Automobile.make_vehicle()。所以,更新代码,


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