bukowski bukowski - 27 days ago 5
Python Question

Python 2 Odd or Even numbers

I'm trying to fix a problem like if a number is odd print smth. if even print smth. else. My Python code is as follows ;

import sys
import math


N = int(raw_input().strip())

def dec(num):
if num % 2 == 0 and num != 0:
print 'Not Odd'
elif num == 0:
print 'Case Zero'
else:
print 'Even'

dec(N)


Why I can't compile this code ?

Answer

You have a ' within the string enclosed by 's. Try:

print 'Zero can\'t be odd or even!'

As I see, your indent is bad as well. Pleas align the elif and else below the if.

You also have a not syntactical problem. "Not even" and "Odd" are the two possibilities for you which is bad.

I've corrected these errors for you:

def dec(num):
    if num % 2 == 0 and num != 0:
        print 'Even'
    elif num == 0:
        print 'Zero can\'t be odd or even!'
    else:
        print 'Odd'

for N in range(5):
    dec(N)

One more thing is that you should think about the question about 0 wheter you really want to say that it's not even. Ask your math teacher about this.