多维数组中int的加法

2024-06-28 19:21:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我觉得自己很笨,但如何定位数组的元素并向其中添加一个元素呢?问题是向购物车添加项目和数量。所以我需要在数量上加一个,这是第二个输入。i、 e.多数组将包含[项目][数量]

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]

Tags: theto项目in定位add元素your
1条回答
网友
1楼 · 发布于 2024-06-28 19:21:42

需要注意的事项: 是的,你需要提供额外的代码,这是一个例子。 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)

相关问题 更多 >