[Tutor] TypeError: can't multiply sequence by non-int of type 'float'

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Apr 4 16:30:26 CEST 2013


On 4 April 2013 14:40, Sayan Chatterjee <sayanchatterjee at gmail.com> wrote:
> Dear All,
>
> I know this error occurs when one tries to multiply a string with a fraction
> i.e float. In my case , I can't figure out how can a numpy floating point
> array be a string.  Interestingly, the concerned expression gets printed in
> the loop for the first time but on the second go it fails.
>
> The concerned snippet of code is given below :
>
> for t in t_range(0,1,0.1):
>   print t
>   p_za=[]
>   pv_za=[]
>
>   # Opening file in file_t format
>   fname = 'file_' + str(t) + '.dat'
>   fo = open(fname,'w')
>
>   # p_za.append(p_initial - t*K*np.sin(K*p_initial))
>   print 'K=',K

K is somewhere above set to a float with the value 3.14...

>   print 'pv_za_temp =',pv_za_temp

pv_za_temp is somewhere above set to a list containing an array as
it's only element.

>   print '- t*K*np.sin(K*p_initial) = ',- t*K*np.sin(K*p_initial)
>   print '-K*np.sin(K*pv_za_temp) = ',-K*np.sin(K*pv_za_temp)

Here you try to multiply the two. The error can be achieved like so:

>>> import numpy
>>> a = numpy.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> 3.14 * a
array([[  3.14,   6.28],
       [  9.42,  12.56]])
>>> 3.14 * [a]

Perhaps what you want is to multiply K with pv_za_temp[0]?

[SNIP]
> K= 3.14159265359
> pv_za_temp = [array([[ -0.00000000e+00,  -3.14159265e+00,   6.49753967e-13,
>           3.14159265e+00,  -1.29950793e-12]])]


Oscar


More information about the Tutor mailing list