[Tutor] Mathematical operations on a list

Mark Lawrence breamoreboy at gmail.com
Wed Oct 20 17:40:04 EDT 2021


On 20/10/2021 21:59, JUDE EJEPU wrote:
> Sir,
> I created a function to calculate a variable G
> def gfactor(dist,electro):
>         G = pie_value() * (dist**2 - electro**2)/(2*electro)

Where is the function pie_value defined?

>      return G
> 
> So I tried an example and it worked.
> gFactor(10,2) ----> 75.408
> However, when I passed a list of arguments,:
> x = [1, 2, 3]
> y = [0.5, 0.5, 1]
> gFactor(x,y), I got an error
> 
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
> <ipython-input-7-66124c985163> in <module>
> ----> 1 gFactor(x,y)
> 
> <ipython-input-3-9e0effe1021d> in gFactor(dist, electro)
>       12     Output: ---> float
>       13     """
> ---> 14     G = 3.142 * (dist**2 - electro**2)/(2*electro)

pie_value has disappeared to be replaced by 3.142, please could we have 
some consistency.

>       15     return G
> 
> TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
> 
> 
> Please, how may I correct the function?

You don't, you need to pass in correct Python types for dist and 
electro, whilst you're trying to call it with two lists.

> 
> Thank you.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list