我正在尝试添加 return 0 以防方程式的最终结果为 None 以防万一: TypeError: unsupported operand type(s) for *: 'NoneType' and 'decimal.Decimal' 但它对我不起作用。这样做的原因是没有。售出数量可以为零,因此它返回类型错误。这是功能: def designer_total_discount_price(self): total_sold_discount = self.count_sold * self.discount_price return 0 if total_sold_discount is None else total_sold_discount def designer_total_price(self): total_sold_regular = self.count_sold * self.price return 0 if total_sold_regular is None else total_sold_regular def designer_total(self): if self.discount_price: return self.designer_total_discount_price() return self.designer_total_price() def get_designer_final(self): return self.designer_total() * self.designer_profit
1 回答
汪汪一只猫
TA贡献1898条经验 获得超8个赞
问题可能self.count_sold在于None。您不能将数字与 相乘None。除了确保self.count_sold始终None可以添加此方法之外:
def get_count_sold(self):
return 0 if self.count_sold is None else self.count_sold
然后你会换self.count_sold出self.get_count_sold(). 您还可以删除 return 语句中的 if 语句。所以像这样:
def designer_total_discount_price(self):
return self.get_count_sold() * self.discount_price
def designer_total_price(self):
return self.get_count_sold() * self.price
我希望它有所帮助!:)
添加回答
举报
0/150
提交
取消