Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复库存程序上的“尝试除外”错误?_Python - Fatal编程技术网

Python 如何修复库存程序上的“尝试除外”错误?

Python 如何修复库存程序上的“尝试除外”错误?,python,Python,我有一个100%有效的库存计划,我需要它永远不会出错。为了防止这种情况,我选择了默认显示消息并重新显示菜单。我相信我的程序try/except是正确的,但是我现在不管你输入什么,它都会显示错误消息。我是怎么修好的?如果你有任何问题,请告诉我。先谢谢你 完整代码: import os name = [] qty = [] class Foo(): def __init__(self, name, qty): self.name = name self

我有一个100%有效的库存计划,我需要它永远不会出错。为了防止这种情况,我选择了默认显示消息并重新显示菜单。我相信我的程序try/except是正确的,但是我现在不管你输入什么,它都会显示错误消息。我是怎么修好的?如果你有任何问题,请告诉我。先谢谢你

完整代码:

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')
    while 1:
        try:
            CHOICE = int(input("Enter choice: "))
            menuDisplay(CHOICE)
            if CHOICE == 1:
                while 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: ')
                    pos = name.index(search) if search in name else -1
                    if (pos >= 0):
                        print ('Item:     ', name[pos])
                        print ('Quantity: ', qty[pos])
                        print ('----------')
                    else:
                        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('=================')
                    zipo = list(zip(name, qty))
                    for foobar in zipo:
                        print("Item : ",foobar[0], "\nQuantity : ",foobar[1])
                        print('----------')
                    CHOICE = int(input('Enter 98 to continue or 99 to exit: '))
                if CHOICE == 98:
                    menuDisplay()
                elif CHOICE == 99:
                    exit()
                else:
                    print("Please enter a number between 1 and 5")
                    menuDisplay()
        except Exception as e:
            print("Invalid choice! try again!"+str(e))

menuDisplay()
导入操作系统
名称=[]
数量=[]
类Foo():
定义初始化(自身、名称、数量):
self.name=名称
自身数量=数量
def menuDisplay():
打印('========================================')
打印('=库存管理菜单=')
打印('========================================')
打印(‘(1)将新项目添加到库存’)
打印(‘(2)从库存中删除项目’)
打印(‘(3)更新库存’)
打印(‘(4)库存中的搜索项目’)
打印(‘(5)打印库存报告’)
打印(‘(99)退出’)
而1:
尝试:
CHOICE=int(输入(“输入选项:”)
菜单显示(选项)
如果选项==1:
而1:
打印('添加库存')
打印('======================')
new_name=input('输入项的名称:')
name.append(新名称)
新建数量=整数(输入(“输入项目数量:”)
追加数量(新增数量)
CHOICE=int(输入('输入98继续,或输入99退出:'))
如果选项==98:
menuDisplay()
elif选项==99:
退出()
elif选项==2:
打印('删除库存')
打印('======================')
删除=输入('输入要从库存中删除的项目名称:')
indexdel=name.index(正在删除)
name.pop(indexdel)
pop数量(indexdel)
CHOICE=int(输入('输入98继续,或输入99退出:'))
如果选项==98:
menuDisplay()
elif选项==99:
退出()
elif选项==3:
打印('更新库存')
打印('======================')
项目=输入('输入要更新的项目:')
update=int(输入(“输入更新的数量。输入5表示额外数量,输入-5表示更少:”)
如果更新>=0:
数量[名称索引(项目)]+=更新
打印(“已更新”)
CHOICE=int(输入('输入98继续,或输入99退出:'))
如果选项==98:
menuDisplay()
elif选项==99:
退出()
elif更新=0):
打印('项目:',名称[pos])
打印('数量:',数量[pos])
打印('------------')
其他:
打印(“不在库存中的项目”)
CHOICE=int(输入('输入98继续,或输入99退出:'))
如果选项==98:
menuDisplay()
elif选项==99:
退出()
elif选项==5:
打印('当前库存')
打印('======================')
zipo=列表(邮政编码(名称、数量))
淄博福巴:
打印(“项:”,foobar[0],“\n项:”,foobar[1])
打印('------------')
CHOICE=int(输入('输入98继续,或输入99退出:'))
如果选项==98:
menuDisplay()
elif选项==99:
退出()
其他:
打印(“请输入一个介于1和5之间的数字”)
menuDisplay()
例外情况除外,如e:
打印(“无效选择!重试!”+str(e))
menuDisplay()

也许你可以尝试一下解决这个问题,除了

x = input("enter digit: ")
try:
    x = int(x)
except ValueError:
    print(f"{x} is not an Integer")
    # Handle error in input 

print(x)

听说过吗?有一种叫做的东西可以做你想做的事。它必须是代码而不是下载的额外文件。你明白我为什么这么建议吗?它是用来测试你的代码并对其进行修改的,你实际上并没有把它包括在你的最终代码中。看起来缩进是错误的。将所有内容从
elif CHOICE==2移动到
,除了还剩4个空格。我想我已经解决了,你能看看当前的问题吗?你正在尝试将一个参数传递给
menuDisplay()
,而函数不带参数。请尝试注释这一行。引用的是哪一行?@user14391204,第27行