简体   繁体   中英

TypeError: unsupported operand type(s) for *: 'float' and 'set'

import math


def cal(v1, v2, dt, m, rho, A, D):
    k = -D * rho * A * dt / 2 / m

    ve = {-k*(v1+v2) + math.sqrt(math.pow((k*(v1+v2)),2)-4*k*{k*v1*v2+v1-v2})}/(2*k)
    return ve

m = 16.2*10**-3
rho = 1.225

A = 2*10**-4
D = 0.47


v1 = 0.350313
v2 = 0.301598
dt = 1.329709


ve = cal(v1, v2, dt, m, rho, A, D)

print(ve)

From executing this code, I got error: TypeError: unsupported operand type(s) for *: 'float' and 'set'

How can I solve this problem?

It seems you are creating a set by using the { } braces within the ve operation.

And because you are trying to use multiplication, and the multiplication operation is between a float (left hand side k ) and the set, it fails as it is unsupported.

Remove the {} inside your equation and you should be fine.

Change following line

ve = {-k*(v1+v2) + math.sqrt(math.pow((k*(v1+v2)),2)-4*k*{k*v1*v2+v1-v2})}/(2*k)

to

ve = (-k*(v1+v2) + math.sqrt(math.pow((k*(v1+v2)),2)-4*k*(k*v1*v2+v1-v2)))/(2*k)

OUTPUT :

-3.536702464867566

In python values enclosed inside { } are either set or dictionary, and inside [ ] are lists. You should always use small parenthesis ( ) to serve the purpose of brackets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM