Home > database >  Calculation problem in a function in Python
Calculation problem in a function in Python

Time:11-09

I'm trying to calculate something in a function with numbers from an imported list.

def calculate_powers(velocities_list):
    power_list = []
    for i in velocities_list:
        power = 8/27 * 1.225 * i * 8659
        power_list.append(power)
    print(power_list)

However, I get the following error:

File "C:\Users\Omistaja\PycharmProjects\7_1\wind_turbine.py", line 6, in calculate_powers
    power = 8/27 * 1.225 * i * 8659
TypeError: can't multiply sequence by non-int of type 'float'

What to do?

CodePudding user response:

try this :

def calculate_powers(velocities_list):
    power_list = []
    for i in velocities_list:
        power = float(8/27) * 1.225 * float(i) * float(8659)
        power_list.append(power)
    print(power_list)

CodePudding user response:

your variable 'i' is probably string, you need to cenvert it to number(float or int,..)

power = 8/27 * 1.225 * float(i) * 8659

can you provide example of your velocities_list?

  • Related