我有這個字典,其中包含一些可以添加到購物車的產品的值。我想匯總購物車中所有產品的價格并列印出來。如何使用函式從字典中選擇特定值(價格)。在這種情況下,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()
uj5u.com熱心網友回復:
首先,您需要在某個地方保存產品目錄,如下所示:
catalog = [shoeA, shoeB]
然后,您可以使用一個簡單的 for 回圈來查找乘積的總和:
def total_price():
sum = 0
for product in catalog:
sum = product['price'] * cart.count(product['id'])
return sum
uj5u.com熱心網友回復:
首先,您必須將所有產品存盤在串列或字典中。
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()
uj5u.com熱心網友回復:
將鞋類產品的宣告變成產品字典。我更新了其他函式以反映這種變化。
products_dict = {342453 : {"name": "Shoe A", "price": 144.98 }, 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_id, times):
for i in range(0, times):
cart.append(products_dict[product_id])
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(product_id, times):
for i in range(0, times):
cart.remove(products_dict[product_id])
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(product_id):
for index, product in enumerate(cart):
if product == products_dict[product_id]:
print(index)
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. :)
sum = 0
for product in cart:
sum = product["price"]
print(sum)
get_total()
uj5u.com熱心網友回復:
我建議首先嘗試一種更易于管理的產品結構。就像一個嵌套的字典
catalog = dict(
shoeA={"id": 342453, "name": "Shoe A", "price": 144.98 },
shoeB={"id": 9800989, "name": "Shoe B", "price": 300}
)
這意味著整個目錄都可以呼叫。您的代碼的問題是每個產品都是分開的,因此不容易呼叫。在嵌套字典中,我們可以輕松呼叫 product 變數,因為它只是一個字串。
然后,您編輯 add 函式以適應不是產品的內部詳細資訊(在您的代碼中就是這種情況),而是“目錄”鍵(產品)。未來的函式應該呼叫這個鍵(例如“shoeA”或“shoeB”)
cart = []
cart_id = []
def add_product(product, times):
for i in range(0, times):
cart.append(product)
cart_id.append(catalog[product]['id'])
print(cart)
print(cart_id)
add_product('shoeA', 4)
['shoeA', 'shoeA', 'shoeA', 'shoeA']
[342453, 342453, 342453, 342453]
在這一部分,我們使用了可呼叫的嵌套字典。現在您可以輕松自由地控制您想要的產品的所有屬性。例如,如果您想保留 ID 串列,您可以使用cart_id代替購物車。
現在這種格式可以很容易地進行求和。
def get_total():
cart_prices = [catalog[product]['price'] for product in cart ]
total = sum(cart_prices)
print(cart_prices)
print(total)
get_total()
這應該有效。隨意調整其他功能以接受新結構。
uj5u.com熱心網友回復:
要處理提取的產品資料(檔案、api 或資料庫),如果它們具有固定且可用的大小,您可以以這種方式存盤它們:
product_dict = {product["id"]:product for product in products}
然后像這樣迭代購物車串列以回傳總和:
def get_total():
amount = 0
for product_id in set(cart):
amount = product_dict[product_id]["price"] * cart.count(product_id)
return amount
print(get_total())
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/351730.html
標籤:Python
