Python Forum

Full Version: Eulers Number Exercise
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everbody, I'm really struggling with this excercise. Can somebody help me?

e (Eulers number) = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! ⋯ + 1/n!

Try to get our own e . You need to write a function "factorial" to calculate the factorial number given the n and then use "for loop" to generate the e . Let n go from 0 to 10, and save the derived value of e in a list and print it out when n is an odd number (up to 10).

Hints: Consider use range(#begin, #end) method
What have you tried?
Thank you for your answer :-)

I just started learning python...

I tried:

def factorial:
n = (1/math.factorial(i))
 if i%2 != 0:
    print(numbers+(1/math.factorial(i))
and


numbers = [x for x in range(11)]
if i in numbers:
    if i%2 != 0:
    print(numbers+(1/math.factorial(i))
You've got some good ideas, what I suggest:
Set e to zero
Create a for loop with a range of 11
In that loop, calculate 1/n!, add it to your e
Check to see if n is odd - if so, print your current value of e

When in doubt, write out in plain english what you believe the steps to be. Then code those steps. Google is your friend if you don't know how to code a particular step, or post here. We will help with a step, but obviously won't do the whole things for you
You're also told to write your own function for computing the factorial, instead of using a library one..