多维数组中int的加法

问题描述 投票:0回答:1

我真的很傻,但是如何找到数组的元素并添加一个元素呢?问题是将项目和数量添加到购物车中。所以我需要在数量上添加一个,这是第二个输入。即多数组将包含[item] [quantity]

else:
    print ("That item is already in your cart")
    add = input("Want to add one more to your cart?")
    if add == "yes":
        addItem(strChoice)



def addItem(strChoice):
    #TO DO
    #Find the strChoice in the aryCart and then add one to the qty 
    for i in strChoice:
      aryCart = [strChoice][i+1]
python
1个回答
-1
投票

一些注意事项:是的,您需要提供额外的代码,这是一个示例。aryCart必须是字典,而不是数组。下面是一个示例,它是如何完成的。

aryCart = {'a':1,'b':2,'c':2}
print(aryCart)
def addItem(strChoice):
    #TO DO
    #Find the strChoice in the aryCart and then add one to the qty 
    for i in aryCart:
        if(i == strChoice):
            aryCart[i] = aryCart[i]+1

print ("That item is already in your cart")
add = input("Want to add one more to your cart?")
strChoice = input()
if add == "yes":
    addItem(strChoice)  # Whatever your strChoice is 

print(aryCart)
© www.soinside.com 2019 - 2024. All rights reserved.