Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 24355

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

$
0
0

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!


Viewing all articles
Browse latest Browse all 24355

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>