Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Nested Lists_Calculation_Python 3.9 - Fatal编程技术网

Python 从嵌套列表中减去整数

Python 从嵌套列表中减去整数,python,list,nested-lists,calculation,python-3.9,Python,List,Nested Lists,Calculation,Python 3.9,•如果有足够的糖果出售,系统必须从数量[项目2]中减去输入的金额,以便显示商店中剩余的可用糖果数量) •如果没有足够的糖果可供出售(客户希望购买的糖果超过可用数量),系统必须通知用户无法进行交易。需要在def功能内完成,任何人都可以提供帮助,谢谢 lst_s = [ ["Smarties", 5, 37], ["Cookie", 8, 80], ["Fizzies", 4, 50], ["Chocolate bar"

•如果有足够的糖果出售,系统必须从数量[项目2]中减去输入的金额,以便显示商店中剩余的可用糖果数量) •如果没有足够的糖果可供出售(客户希望购买的糖果超过可用数量),系统必须通知用户无法进行交易。需要在def功能内完成,任何人都可以提供帮助,谢谢

lst_s = [
["Smarties", 5, 37], ["Cookie", 8, 80], ["Fizzies", 4, 50], ["Chocolate bar", 4, 25]
]

def update_quantity():
    name = str(input("Enter name of confectionary: "))
    price = str(input("Enter price: "))
    amount = int(input("enter quantity customer wants to buy"))

    for i in range(len(lst_s)):
        item = lst_s[i]
        iamount = item[2]
        if amount > iamount:
            print("Sorry not enough in store to sell")
        else:
            iamount = iamount - amount
            print(lst_s)
如果我将名称输入为Cookie,将价格输入为8,将数量输入为3,则所需的输出将为:

["Smarties", 5, 37], ["Cookie", 8, 77], ["Fizzies", 4, 50], ["Chocolate bar", 4, 25]
由于3的数量已从原始饼干数量中扣除,请尝试以下操作:

def update_quantity():
  name = str(input("Enter name of confectionary: "))
  price = str(input("Enter price: "))
  amount = int(input("enter quantity customer wants to buy"))

  for i in range(len(lst_s)):
    item = lst_s[i]
    item_name = item[0]
    iamount = item[2]
    if name != item_name:
        continue
    if amount > iamount:
        print("Sorry not enough in store to sell")
    else:
        lst_s[i][2] = iamount - amount
        print(lst_s)

应该是相对简单的。您可能希望使用dict存储产品

lst\u s=[
[“Smarties”,5,37],
[“Cookie”,8,80],
[“汽水”,4,50],
[“巧克力棒”,4,25],
]
def更新_数量():
name=str(输入(“输入糖果店名称:”)
价格=str(输入(“输入价格:”)
金额=整数(输入(“输入客户想要购买的数量”))
对于lst_s中的产品:
如果产品[0]==名称:
如果金额>产品[2]:
打印(“对不起,商店里没有足够的东西出售”)
其他:
产品[2]=产品[2]-金额
打破
打印(lst\U s)
更新_数量();