我有一个具有5个不同选项的清单程序。 1-3工作,不久将有五个工作。选项4 "Search Inventory"是问题。通过选项1将项目添加到程序后,键入98,然后输入4。

这是完整的代码:

import os


name = []

qty = []

class Foo():
    def __init__(self, name, qty):
        self.name = name
        self.qty = qty

def menuDisplay():
    print ('=============================')
    print ('= Inventory Management Menu =')
    print ('=============================')
    print ('(1) Add New Item to Inventory')
    print ('(2) Remove Item from Inventory')
    print ('(3) Update Inventory')
    print ('(4) Search Item in Inventory')
    print ('(5) Print Inventory Report')
    print ('(99) Quit')
    CHOICE = int(input("Enter choice: "))
    menuSelection(CHOICE)

def menuSelection(CHOICE):

    if CHOICE == 1:
        print('Adding Inventory')
        print('================')
        new_name = input('Enter the name of the item: ')
        name.append(new_name)
        new_qty = int(input("Enter the quantity of the item: "))
        qty.append(new_qty)
        CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
    if CHOICE == 98:
        menuDisplay()
    elif CHOICE == 99:
        exit()
    elif CHOICE == 2:
        print('Removing Inventory')
        print('==================')
        removing = input('Enter the item name to remove from inventory: ')
        indexdel = name.index(removing)
        name.pop(indexdel)
        qty.pop(indexdel)
        CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
    if CHOICE == 98:
        menuDisplay()
    elif CHOICE == 99:
        exit()
    elif CHOICE == 3:
        print('Updating Inventory')
        print('==================')
        item = input('Enter the item to update: ')
        update = int(input("Enter the updated quantity. Enter 5 for additional or -5 for less: "))
    if update >= 0:
        qty[name.index(item)] += update
        print("Update made")
    CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
    if CHOICE == 98:
        menuDisplay()
    elif CHOICE == 99:
        exit()
    elif update <= -1:
        qty[name.index(item)] -= update
        print("Update Made")
        CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
    if CHOICE == 98:
        menuDisplay()
    elif CHOICE == 99:
        exit()
    elif CHOICE == 4:
        print('Searching Inventory')
        print('===================')
        search = input('Enter the name of the item: ')
        if search == name:
            print ('Item:     ', name.index(search))
            print ('Quantity: ', qty.index)
            print ('----------')
        if search != name:
            print("Item not in inventory")
    CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
    if CHOICE == 98:
            menuDisplay()
    elif CHOICE == 99:
        exit()
    elif CHOICE == 5:
        print('Current Inventory')
        print('=================')
        input('Enter the name of the item: ')
        CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
    if CHOICE == 98:
        menuDisplay()
    elif CHOICE == 99:
        exit()
        printInventory()
    elif CHOICE == 99:
        exit()

menuDisplay()
分析解答

不需要输入带有选项#1的项目,然后在选择选项#4之前键入98。如果您要做的只是立即选择选择#4,则会出现问题。

这样做的原因是,输入4将允许代码到达此行:

if update >= 0:

该行引发异常,因为尚未将任何值分配给update。查看代码,唯一给update赋值的地方是当代码正在处理用户的3条目时,此处:

elif CHOICE == 3:
    print('Updating Inventory')
    print('==================')
    item = input('Enter the item to update: ')
    update = int(input("Enter the updated quantity. Enter 5 for additional or -5 for less: "))

由于从未选择过该选项,因此从未设置update,因此您会看到异常。

我对您的逻辑不太了解,无法确定应该如何解决。可能在menuSelection函数的开始处将0的值分配给update