微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Python 代码中更新清单的问题

如何解决在 Python 代码中更新清单的问题

我正在编写一个班级期末考试的程序,但在更新创建的库存时遇到了一些问题。我已经能够完成代码中的所有其他操作,但是当我运行更新部分时,没有任何更新。我真的很感激这方面的任何帮助。我所有的代码都包含在下面

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,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','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 条件是错误的,而且您正在插入类 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')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。