【问题标题】:Trying to set up a simple inventory management program尝试建立一个简单的库存管理程序
【发布时间】:2018-06-09 14:07:37
【问题描述】:

我需要设置一个仅管理 3 种产品库存的程序(选择 1 只是打印关于所有库存的数量、价格或两者的事实,选择 2 是打印关于特定产品的信息,我还没有得到到第三部分了)。

问题:一旦我使用input 输入1 和3 之间的操作,没有其他输出,但没有错误。

products = [['pen', 'pencil','notebook'], [10,20,30],[1, .5, 2]]
total_number_items = products[1][0] + products [1][1] + products[1][2]
min_num = min(products[1])
max_num = max(products[1])

print('1.Generate overall statistics for the entire inventory (total number 
of items, average price per item, the minimum number of items among the 
products and the maximum number of items among the products')
print('2.Retrieve information (quantity or price) for a given product')
print('3.Update information (quantity or price) for a given product')
choice = input('Select an operation by entering its number: ')


if choice == 1:
    print ('Total number of items in inventory is: ',total_number_items)
    print ('The minimum number of items among the products is: ', min_num)
    print ('The maximum number of items among the products is: ', max_num)



if choice == 2:
    inquiry = input('Which product would you like information about? ')
    if inquiry == 'pen' or 'Pen' or 'PEN':
        inquiry2 = input('Would you like quanity, price, or both? ')
        if inquiry2 == 'Quantity' or 'quantity' or 'QUANTITY':
            print('Quantity of pens is', products[1][1])
            if inquiry2 == 'price' or 'Price' or 'PRICE':
                print ('Price of pens is', products[2][1])
                if inquiry2 == 'both' or 'Both' or 'BOTH':
                    print ('Quantity of pens is', products[1][1], 'Price of 
pens is', products[2][1])

if inquiry == 'pencil' or 'Pencil' or 'PENCIL':
    inquiry2 = input('Would you like quanity, price, or both? ')
    if inquiry2 == 'Quantity' or 'quantity' or 'QUANTITY':
        print('Quantity of pencils is', products[1][1])
        if inquiry2 == 'price' or 'Price' or 'PRICE':
            print ('Price of pencils is', products[2][1])
            if inquiry2 == 'both' or 'Both' or 'BOTH':
                print ('Quantity of pencils is', products[1][1], 'Price of pencils is', products[2][1])

if inquiry == 'notebook' or 'Notebook' or 'NOTEBOOK':
    inquiry2 = input('Would you like quanity, price, or both? ')
    if inquiry2 == 'Quantity' or 'quantity' or 'QUANTITY':
        print('Quantity of notebooks is', products[1][1])
        if inquiry2 == 'price' or 'Price' or 'PRICE':
            print ('Price of notebooks is', products[2][1])
            if inquiry2 == 'both' or 'Both' or 'BOTH':
                print ('Quantity of notebooks is', products[1][1], 'Price of notebooks is', products[2][1])

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    input 在 python 中读取并返回一个 string,但在您的 if 语句中,您询问接收到的输入是否等于 整数

    简单而冒险的方法,使用int()将字符串转换为整数:

     choice = int(input("pick a number: "))
    

    现在,如果用户输入不是 int 的内容,显然您会遇到麻烦...为避免这种情况,您可以捕获错误:

    try:
        choice = int(input("pick a number: "))
    except ValueError:
        # not an int...
        print("Oops, I asked for an integer...")
    

    或者您可以预先检查输入是否仅由数字组成:

    answer = input("pick a number: ").strip() # remove spaces if any
    if answer.isdigit():
        choice = int(answer)
    else:
        print("Oops, I asked for an integer...")
    

    另一个错误。当你写:

    if inquiry == 'Pencil' or 'pencil' ...
    

    实际上这意味着

    if (inquiry == 'Pencil') or ('pencil' is True) or ...
    

    由于非空字符串在转换为布尔值时为True,因此 if 将始终执行。所以,改为这样写:

    if inquiry == 'Pencil' or inquiry == 'pencil' or ...
    

    或者:

    if inquiry in ['Pencil', 'pencil', ... ]
    

    更好的是,将inquiry 设为小写,这样您只需检查pencil

    if inquiry.lower() == 'pencil'
    

    【讨论】:

    • 当我修复该错误并选择选项 2 时,无论我要求什么,它都会打印出数量、价格以及两者。你知道我该如何解决这个问题吗?
    【解决方案2】:

    python3.x 中的 input() 语句会将值作为 string ,当您尝试将 string 与 int 进行比较时,比较将失败

    >>> i = input('enter a number: ')
    enter a number: 2
    >>> type(i)
    <class 'str'>
    >>>
    >>>
    >>> i = int(input('enter a number: '))
    enter a number: 2
    >>> type(i)
    <class 'int'>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-18
      • 2011-10-15
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 2010-09-30
      • 2019-06-19
      • 1970-01-01
      相关资源
      最近更新 更多