Zach Thomas - 1 year ago 129

Python Question

I defined a method below that is supposed to take 5 integer/float values as input, and one list object (the

`lst`

`r`

`F`

I got the error

`Error : Can't multiply sequence by non-int of type 'Mul'`

`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)

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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.

Here's the corrected code.

```
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)
```

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.

Edit:
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]
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**