I need to create a function that correctly computes E m as E m = 1 - mE m-1
I'm having some issues figuring out why my variable isn't returning the correct values. Here is a summary of the problem I am tackling
Write a function called integration that takes as input the integer m
and, using the algorithm described below, returns the value of E_m
. Make certain that you index this correctly, ie E 0 = 1 − 1/e not E1 = 1 − 1/e. Be very careful with how Python indexes if you try to do this as an indexed array.
Set E = 1 - 1/e
for j = 1, ... , m do
E = 1 - j*E
end for
return E
Here is my code
import numpy as np
def integration(m):
E_m = 1 - 1/np.e
for j in range(m):
E_m = 1 - j*E_m
return E_m
My code, if m > 0
, returns the wrong answer (for example integration(1)
returns 1
, when it should be 1 - 1*(1-1/e)
). The text seems to indicate that I should use an array, but I don't see why I would need to. Why isn't my code redefining my variable E_m correctly? I'm new to Python, so I'm not sure if it's a problem with having the variable name in the redefinition or not.
Python loops start with 0 and are non-inclusive. Try range(1, m+1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.