错误:无法将序列乘以'Mul'类型的非整数 [英] Error : Can't multiply sequence by non-int of type 'Mul'
问题描述
我在下面定义了一种方法,该方法假定将5个整数/浮点值作为输入,并使用一个列表对象(lst
参数),并且仅返回方程式F
中r
的正解. F的公式应返回一个列表,其中使用给定的方程对每个元素进行乘/减.
I defined a method below that is supposed to take 5 integer/float values as input, and one list object (the lst
parameter) and return only the positive solutions for r
in the equation F
. The formula for F should return a list where each element is multiplied/subtracted using the equation given.
我收到了上面提到的错误Error : Can't multiply sequence by non-int of type 'Mul'
.我想念什么?
I got the error Error : Can't multiply sequence by non-int of type 'Mul'
mentioned above. What am I missing?
from sympy import *
from math import pi
class solarheating:
def circular_rr(T0,T,lst,m,Cp,Eg):
r = var('r')
F = T0+pi*r**2*Eg/(m*Cp)*lst-T
r = solve(F,r)
return r[1]
lst = [0,0.25,0.5,0.75,1.0,1.25]
a = circular_rr(20,15,lst,.24,4.2,.928)
print(a)
推荐答案
您忘记将在其上调用代码的对象(或self
)的实例传递给方法.由于没有self
,因此会出现非整数错误,因为T0变为self
,T
变为T0
,然后将您的lst
用作T
.由于您不能从列表中减去int,因此会产生错误.
You forgot to pass the instance of the object (or self
) on which the code is being called to the method. Since there was no self
, you were getting the non-int error, because T0 became self
, T
became T0
, and then your lst
was being used as T
. Since you cannot subtract a int from a list, this produced an error.
这是更正的代码.
from sympy import *
from math import pi
class solar heating:
def circular_rr(self,T0,T,lst,m,Cp,Eg):
r = var('r')
F = T0+pi*r**2*Eg/(m*Cp)*lst-T
r = solve(F,r)
return r[1]
lst = [0,0.25,0.5,0.75,1.0,1.25]
my_instance = solarheating()
a = my_instance.circular_rr(20,15,lst,.24,4.2,.928)
print(a)
您需要使用self
的原因是因为Python不使用@syntax来引用实例属性.在您通过self
属性传递的实例上调用该方法.您实际上并没有在Python中编写实例方法.您编写的是(必须)将实例作为第一个参数的类方法.因此,您必须将实例参数明确地放置在某个地方.
The reason you need to use self
is because Python does not use the @syntax to refer to instance attributes. The method is called on the instance you pass in the self
attribute. You don't actually write instance methods in Python; what you write is class methods which (must) take an instance as a first parameter. And therefore, you’ll have to place the instance parameter somewhere explicitly.
除了上面已经描述的明显的self
错误之外,您还需要将浮点数乘以一个列表.这就是导致can't multiply sequence by non-int of type 'Mul'
错误的原因.
Besides the obvious self
error I've described above, you're multiplying a float by a list. That's what's causing the can't multiply sequence by non-int of type 'Mul'
error.
如果要将列表中的每个项目乘以浮点数,则更正后的方法代码将为:
If you want to multiply each item in the list by the float, your corrected method code will be:
def circular_rr(self,T0,T,lst,m,Cp,Eg):
r = var('r')
temp_val = T0+pi*r**2*Eg/(m*Cp)
F = [x*temp_val-T for x in lst]
r = solve(F,r)
return r[1]
这篇关于错误:无法将序列乘以'Mul'类型的非整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!