试图建立一个简单的库存管理程序

时间:2018-06-09 05:43:22

标签: python python-3.x

我需要设置一个程序来管理只有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])

2 个答案:

答案 0 :(得分:0)

python中的

input读取并返回字符串,但在您的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'

答案 1 :(得分:0)

python3.x中的input()语句将值作为字符串,当您尝试将字符串与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'>