I have a function that I've written out and would like to perform mathematical operations on it, the same way I can with numbers.
For instance, with the code below, I would like to take Sup(3) - Sup(2) = result, but this doesn't work. Can I take functions that I've defined and perform mathematical operations on them, the same way we can perform mathematical operations on numbers (i.g, 2 * 2 = 4)?
For n = 2, my result is 1.083 For n = 3, my result is 1.717 using the code below.
def Sup(n):
mylist = []
for n in range (2,2**n+1):
Su = (1/n)
mylist.append(Su)
#print(mylist)
print (sum(mylist))
When I attempt this operation, I get the following error:
---> 12 Sup(2)- Sup(3)
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'neType'
What does this mean?
Can I take functions that I've defined and perform mathematical operations on them, the same way we can perform mathematical operations on numbers? Yes you can, assuming that your functions return numbers.
What does this mean?
As pointed out in comments, it means that your function doesn´t return anything. Adding return
to your function should do the trick:
def Sup(n):
mylist = []
for n in range (2,2**n+1):
Su = (1/n)
mylist.append(Su)
#print(mylist)
print (sum(mylist))
return sum(mylist)