我已经能够使用flask Sessions成功添加到购物篮中,但是在计算所有产品的总价时遇到了麻烦。我试图设置一个变量,该变量使用列表中的值来计算价格,但不起作用,而是返回第一个项目。有人可以指出我正确的方向。我也有一种感觉,那就是有更好的方法可以做到这一点,任何人都知道,然后他们可以让我知道,因为这是我的烧瓶应用程序之一,非常有帮助,谢谢。
def _local_repository_impl(repository_ctx):
ws_dir = repository_ctx.path(repository_ctx.attr._workspace).dirname
答案 0 :(得分:0)
因此,您的列表如下:
sVars = [
['apple', 2.12],
['orange', 3.13],
]
我们只需要每个列表的第二个元素的列表,这样我们就可以执行sum([2.12, 3.13])
并获得结果。整洁的方法是使用列表理解:
total_price = sum([x[1] for x in sVars])
print(total_price)
(根据我的拙见)这是一种更整洁的方式:
total_price = 0
for entry in sVars:
total_price += entry[1]
print(total_price)
作为第二要点-与使用svars
像sVars
而不是_
那样使用total_price
进行间隔的totalPrice
相比,Python通常建议变量全部为小写CamelCasing
并将MenuItem
保留为类名,就像使用# command-line-arguments
/usr/bin/ld: $WORK/b001/_x002.o: in function `_cgo_51159acd5c8e_Cfunc_hello':
/tmp/go-build/cgo-gcc-prolog:48: undefined reference to `hello'
collect2: error: ld returned 1 exit status
一样。它将使您的代码更易于其他人查看和使用。