首页 > 解决方案 > Python,浮点错误:+ 的不支持的操作数类型:“浮点”和“列表”

问题描述

我设置了以下课程:

class StatoPatrimoniale(models.Model):
    reference_date=models.DateField()
    cassa=models.DecimalField()

我设置了以下功能:

def stato_patrimoniale(request):
    now=datetime.datetime.now()
    last_account_year=float(now.year)-1
    list_diff=[]
    list_diff = float(StatoPatrimoniale.objects.filter(reference_date__year=last_account_year).values_list('cassa')[0][0])

但是python给了我以下错误:

 unsupported operand type(s) for +: 'float' and 'list'
 list_diff = float(StatoPatrimoniale.objects.filter(reference_date__year=last_account_year).values_list('cassa')[0][0])

为什么?问题出在哪里?

标签: pythonpython-3.xdjangodjango-modelsdjango-views

解决方案


我重现了错误:

TypeError: unsupported operand type(s) for +: 'float' and 'list'

但不是上面共享的代码。相反,我提出:

list_diff = float() + list_diff

list_diff 是一个列表,而 float() 是浮点数,你不能那样做

将其替换为

list_diff.append(float())

推荐阅读