Monday, December 23, 2019

IslandT: Return an even number based on the Nth even number with python

In this example, we will create a python function that will return an even number based on the Nth even number given.

Let say when we enter one into that function, the function will return 0 because the first even number is 0. If we enter two into that function, the function will return 2 because the second number of even numbers is 2. Besides that, we will also need to take care of the number that is smaller than 1 which is an invalid entry as one is the very first even number.

def nth_even(n):
        if n < 1:
                return 0 # return the first even number if the user has entered an invalid number
        else:
                return 2 * (n-1)

Enter your own solution in the comment box below!



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...