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



我需要设置一个程序来管理仅管理 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中的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'

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'>

最新更新