Python can't multiply sequence by non-int of type 'float'

Question:

I am trying to evaluate a formula, np is numpy:

Ds = pow(10,5)
D = np.linspace(0, pow(10,6), 100)
alpha=1.44
beta=0.44
A=alpha*(D/Ds)
L=1.65
buf2=L/4.343
buf=pow(-(alpha*[D/Ds]),beta)
value=exp(buf)

and then I will plot this data but I get:

buf=pow(-(alpha*[D/Ds]),beta)
TypeError: can't multiply sequence by non-int of type 'float'

How can I overcome this?

Asked By: y33t

||

Answers:

Change:

buf=pow(-(alpha*[D/Ds]),beta)

to:

buf=pow(-(alpha*(D/Ds)),beta)

This:

[D/Ds]

gives you list with one element.

But this:

alpha * (D/Ds)

computes the divisions before the multiplication with alpha.

You can multiply a list by an integer:

>>> [1] * 4
[1, 1, 1, 1]

but not by a float:

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

since you cannot have partial elements in a list.

Parenthesis can be used for grouping in the mathematical calculations:

>>> (1 + 2) * 4
12
Answered By: Mike Müller
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.