How to pass on the output of a function to a sigmoid's function?

167 Views Asked by At

I am trying to write a sigmoid function that uses a euler's number rounded up to the 5th decimal places (2.71828)

Here's what I got:

from math import e

def e_with_precision(n):
return '%.*f' % (n, e)
print (e_with_precision(5))

def sigmoid(x):
return 1 / (1 + e_with_precision(5)**x)

print(sigmoid(0.5)

When I run the code I got: TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'

The first function returns the rounded up e, when I tested the two functions separately (with some changes) they both worked. But I have no idea how to properly tie them together.

New to coding, please help!

1

There are 1 best solutions below

1
On

As C.Nivis said correctly, e_with_precisionreturns a string (word/sentence), but python can not calculate with this data type. It needs to be a float (floating point number). You can use

def e_with_precision(n):
    return round(e, n) # rounds e to n digits

and it should work.