function definition. invalid index to scalar variable. PYTHON

def fun(x, k):
    return k[0] * x**5 + k[1] * x**4

I want to create a function with a many different coefficients (that code contains only two for example) which I want to pass as a list. Why can't I submit a list? I get an error "invalid index to scalar variable".


Ответы (1 шт):

Автор решения: overtone

If you pass a list type variable to the function, then everything should work. The error looks like you are passing a number to the function. To avoid confusion, you can use type hints, like this: def fun(x: float, k: list):. Еhen the IDE will indicate a possible error (if you are using an IDE ofk)

→ Ссылка