文章内容

2019/6/23 18:19:18,作 者: 黄兵

can't multiply sequence by non-int of type 'decimal.Decimal'

今天在写程序的时候,出现如下错误:

can't multiply sequence by non-int of type 'decimal.Decimal'


出现问题的原因:

不能将非int类型的数据相乘,来得到Decimal数据类型。

这个很好理解,字符串相乘怎么可能得到数字呢?

下面看具体代码:

 number_section = PrivateNumberPropertyValue.query.get_or_404(cart_list['NumberSection'])
    number_section_value = number_section.property_value
    amount = PrivateNumberPropertyValue.query.get_or_404(cart_list['amount']).property_value
    use_time = PrivateNumberPropertyValue.query.get_or_404(cart_list['option_time'])
    use_time_alias_value = use_time.alias_value
    price = number_section.PrivateNumberPrices.first().price
    calculate_price = amount * price * use_time.value_numerical
    result = '%s 号段: %s 个,使用时长:%s ' % (number_section_value, amount, use_time_alias_value)

下面是具体数据类型的截图:

amount是字符串,所以出现了上面错误。


解决方案:

首先需要将相乘的几个参数转换成int数据类型或者Decimal数据类型,具体看精度。

最后修改代码如下:

calculate_price = int(amount) * float(price) * int(use_time.value_numerical)

严格限制了数据类型。


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - can't multiply sequence by non-int of type 'decimal.Decimal'

分享到:

发表评论

评论列表