« Calling one page template inside another | Main | Changing the content type in page templates »

April 20, 2005

Testing counters to see if they are odd or even

The following is a simple Python script to test whether a number is odd or even:

test_figure = float(test_figure)
half_of_it = float(test_figure/2)
#print str(half_of_it)
if int(half_of_it) == float(half_of_it):
  return 'even'
else: return 'odd'

It relies on the fact that casting a floating point number to an integer will round it up or down, and that dividing an odd number by two always gives a float, whereas dividing an even number gives an integer.

Tags: Python

Posted by pj at April 20, 2005 11:14 AM