gpt4 book ai didi

python - 在 Python 的字典中创建特定值的总和

转载 作者:行者123 更新时间:2023-12-02 16:04:14 25 4
gpt4 key购买 nike

我有这个字典,其中包含我对某些可以添加到购物车的产品的值(value)观。我想总结购物车中所有产品的价格并打印出来。我该如何选择使用函数的字典的特定值(价格)。在这种情况下,ShoeA 应该出现在购物车中两次,结果应该是 144.98 + 144.98 = 289.96

这是我的完整代码:

shoeA = {"id": 342453, "name": "Shoe A", "price": 144.98 }
shoeB = {"id": 9800989, "name": "Shoe B", "price": 300}

# A cart of a shop has the following functions. Each functionality should be implemented as a separate function.
# 1.1 add_product(cart, product, times): adds a product "times" times to the cart. The cart is a list and the product a
# dictionary. A product consists of a ID, name and price

cart = []
def add_product(product, times):
for i in range(0, times):
cart.append(product)
print(cart)
add_product(342453, 4)

# 1.2 remove_product(cart, productID): Removes all products with ID = productID from the given cart
# 1.3 print_cart(cart): Prints the whole cart
def remove_product(productID, times):
for i in range(0, times):
cart.remove(productID)
print(cart)
remove_product(342453, 2) #only two reductions.

# 1.4 find_product(cart, productID): Returns a list of integers, which are indexes of the searched product in the cart.
# E.g. find_product(cart, 12) may return [2,5,10] if the cart contains a the product with productID at index 2,5 and 10.
def find_product(productID):
for i, j in enumerate(cart):
if j == productID:
print(i)
find_product(342453)

# 1.5 get_total(cart): Function that sums up the prices of all products in the cart and returns this value
def get_total():
#Please help me here. :)
get_total()

最佳答案

首先,您必须将所有产品存储在列表或字典中。

all_products = {342453: shoeA, 9800989: shoeB}

然后简单地对其进行迭代并将所有价格相加。

def get_total():
total = 0
for productId in cart:
shoe = all_products[productId]
total += shoe["price"]
print("Total: ", total)

完整代码如下:

shoeA = {"id": 342453, "name": "Shoe A", "price": 144.98 }
shoeB = {"id": 9800989, "name": "Shoe B", "price": 300}

# A cart of a shop has the following functions. Each functionality should be implemented as a separate function.
# 1.1 add_product(cart, product, times): adds a product "times" times to the cart. The cart is a list and the product a
# dictionary. A product consists of a ID, name and price

all_products = {342453: shoeA, 9800989: shoeB}
cart = []


def add_product(product, times):
for i in range(0, times):
cart.append(product)
print(cart)
add_product(342453, 4)

# 1.2 remove_product(cart, productID): Removes all products with ID = productID from the given cart
# 1.3 print_cart(cart): Prints the whole cart
def remove_product(productID, times):
for i in range(0, times):
cart.remove(productID)
print(cart)
remove_product(342453, 2) #only two reductions.

# 1.4 find_product(cart, productID): Returns a list of integers, which are indexes of the searched product in the cart.
# E.g. find_product(cart, 12) may return [2,5,10] if the cart contains a the product with productID at index 2,5 and 10.
def find_product(productID):
for i, j in enumerate(cart):
if j == productID:
print(i)
# find_product(342453)

# 1.5 get_total(cart): Function that sums up the prices of all products in the cart and returns this value
def get_total():
total = 0
for productId in cart:
shoe = all_products[productId]
total += shoe["price"]
print("Total: ", total)

get_total()

关于python - 在 Python 的字典中创建特定值的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69873497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com