我想制作一个简单的流程订单,例如: 您以1份抗酸剂的总金额完成了5.33美元的订单 您完成了一份3酸菜的订单,总价为$ 6.99
我的代码是:
<% Product.category.order("created_at asc").last(3).each do |product| %>
<div class="col col-md-4 col-sm-6">
<div class="product-grid">
<div class="image">
<img src= "<%= product.image %>" alt="<%= product.title %> 's image" class="img-fluid">
<span class="overlay">
<span class="product-details">
<%= link_to "View Details", product, class: "linky" %>
</span>
</span>
</div>
<div class="product-discription py-3">
<h4 class="text-center"><%= product.title %></h4>
<p class="text-center"><small><%= product.description %></small></p>
</div>
<div class="product-btn py-2 text-center">
<a href="#" class="btn btn-lg " role="button" aria-pressed="true">ADD TO CART</a>
</div>
</div>
</div>
<% end %>
<% end %>
但是我出错了
TypeError:“ int”对象不可迭代
答案 0 :(得分:0)
据我了解,这似乎是您想要的代码:
total = 0
def process_order(x_list):
global total
for x in x_list:
print(f"You filled an order for {x[1]} {x[0]} for a total of {x[1]* x[2]}")
total = total + x[1] * x[2]
return
x = [("oranges", 4, 3.22),("gummy bears",1,1.99),("sour bites", 3, 2.33), ("antacid", 1, 5.33)]
process_order(x)
print("Total price: ${:.2f}".format(total))
输出:
You filled an order for 4 oranges for a total of 12.88
You filled an order for 1 gummy bears for a total of 1.99
You filled an order for 3 sour bites for a total of 6.99
You filled an order for 1 antacid for a total of 5.33
Total price: $27.19
您需要将total
定义为全局变量,以获取总价格。另外,您可以像for list_element in list
一样遍历python中的列表。如果要创建函数,则最后需要return
语句。在这种情况下,您要做的就是将每个价格添加到global variable
,并打印出每个项目的流程,而无需退货。
答案 1 :(得分:0)
可以使用相同的概念简化代码,而无需使用定义的函数。要解决的第一件事是您的while len(x) > 0
需要减少len(x)
的内容以结束循环。为此,我们可以使用.pop()
。这将删除索引处的项目,我们将使用0
,并将其分配给变量z
。从这里我们可以不更改您的打印语句,我使用其他格式进行设置。然后,我们可以将每个人的总数添加到正在运行的total
中。
x = [("oranges", 4, 3.22),("gummy bears",1,1.99),("sour bites", 3, 2.33), ("antacid", 1, 5.33)]
total = 0
while len(x) > 0:
z = x.pop(0)
print('You filled and order for {} {} for a total of {}'.format(z[1], z[0], z[1] * z[2]))
total += z[1] * z[2]
print("Total price: {:.2f}".format(total))
You filled and order for 4 oranges for a total of 12.88 You filled and order for 1 gummy bears for a total of 1.99 You filled and order for 3 sour bites for a total of 6.99 You filled and order for 1 antacid for a total of 5.33 Total price: 27.19
答案 2 :(得分:0)
函数名称process_order提供了一个想法,即仅处理了一个订单。因此,一次在该处处理许多订单不是一个好主意。此外,如果您从此函数返回订单总计,则可以对其求和以得到总计:total = sum(map(process_order,orders))。这样一来,您可以节省大量时间来改善功能,而总数仍然保持不变。
下面的示例代码:
def process_order(order):
"""Process an order.
Order is a tuple and contains 3 fields: name, quantity and price.
"""
name, quantity, price = order
total_price = price * quantity
print(f"You filled an order for {]} {} for a total of {.2f}".format(
quantity, name, total_price))
return total_price
orders = [
("oranges", 4, 3.22), ("gummy bears", 1, 1.99),
("sour bites", 3, 2.33), ("antacid", 1, 5.33)]
total = sum(map(process_order, orders)) # can be rewritten with for loop
print("Total price: ${:.2f}".format(total))