繁体   English   中英

我的函数 get_subtotal 应该添加所有产品价格,我在循环中做错了什么?

[英]My function get_subtotal should be adding all product price, what I am doing wrong with the loop?

函数get_subtotal - 对每种产品的价格get_subtotal并返回 函数get_tax - 返回小计的 6.5% 函数get_total - 返回小计加上税款
这些都在 Class 里面。

class Order():
    def __init__(self):
        self.id = ""
        self.products = [] 

def get_subtotal(self):
    self.total = 0
    for item in self.products:
        self.total += 1
    return self.total

def get_tax(self):
    return self.total * 0.065

def get_total(self):
    return self.total + self.get_tax()`

尝试更改get_subtotal方法中的get_subtotal ,如果将return语句放在for循环中,它将仅返回第一项!。 但是还是不明白,因为你的方法只计算产品列表的长度,也许是这样的?

def get_subtotal(self):
    self.total = 0
    for item in self.products:
      self.total += item
    return self.total


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM