i'm trying to use the elif statement but every time i try to use it it gives a syntax error.
here's the code:

n = input()


while n != 2:
    n = n - 2
    print "number is even"
elif: n < 2:
    print "number is odd"

Edited 5 Years Ago by vegaseat: added code tags

"There's an error in your program:
invalid syntax"
I edited the program a little bit. Here's it now:

print "input a number"

n = input()


while n != 2 or -2:
    n = n - 2
elif n < 2 or == -2:
    print "number is even"
elif n <= -3:
    print "number is odd"

still same problem

Well, for God sake gives the error message you get!

Are you sure you need while for this job. BTW it has no else part not to talk about elif. And in you new version while condition is always True as <condition> or -2 is always True.

Edited 5 Years Ago by pyTony: n/a

thanx 4 the help the program is almost done. just need to add some final touches and it'll be complete. here's the program:

print "input a number"

n = input()
n = n - 2


if n == 2 or n == -2 or n == 0:
    print "number is even"
else:
    print "press enter again"

f = raw_input()

while n != 2 and n >= -3:
    n = n - 2
if n <= -3: 
    print "number is odd"
elif n == 2 or n == -2:
    print "number is even"

l = raw_input()

Are you sure you need while for this job. BTW it has no else part not to talk about elif. And in you new version while condition is always True as <condition> or -2 is always True.

What is definition of even/odd? is it connected with substraction or division? You need only one if newbie style, one bitwise logical operation geek style.

this is kinda my first real program so its just made to calculate if a number is odd or even by subtracting by 2

What is definition of even/odd? is it connected with substraction or division? You need only one if newbie style, one bitwise logical operation geek style.

It is not super efficient but as one proof of concept it makes sense. I would use function abs for that kind of program and stop while when number <1.

I make it stop at -2 because just in case some one put 0 it will subtract 0 - 2. I dont know how to make it a function. I would appreciate it if u told me.

It is not super efficient but as one proof of concept it makes sense. I would use function abs for that kind of program and stop while when number <1.

Ok, I do one exception on no ready code for this small snippet, if you promise to learn something and do another nice project immediately:

def odd_by_sub(x):
    x = abs(x)
    while x > 1:
        x -= 2
    return bool(x)

for t in (-4, 5, 1, 0, -1, 98792743):
    print(t, odd_by_sub(t))

The newbie way with modulo:

´def odd_by_mod(x):
    if x % 2 == 0:
        return False
    else:
        return True

for t in (-4, 5, -5, 1, 0, -1, 98792743):
    print('%i odd: %s' % (t, odd_by_mod(t)))

The expert's way

def odd_by_and(x):
    return bool(x & 1)

for t in (-4, 5, 1, -5, 0, -1, 98792743):
    print(t, odd_by_and(t))

Another impractical (in Python especially without tail recursion removal) by recursion:

def odd_by_rec(x):
    if x < 0 :
        return odd_by_rec(-x)
    elif x < 2:
        return bool(x)
    else:
        return odd_by_rec(x-2)

# recursion in Python can not deal with too deep recursion so smaller values only
for t in (-4, 5, 1, 0, -1, 743):
    print(t, odd_by_rec(t))

Edited 5 Years Ago by pyTony: n/a

to tell u the truth i dont really understand the code u sent me. but i'll get it. all i really know how to program in python is the most basic stuff.

Ok, I do one exception on no ready code for this small snippet, if you promise to learn something and do another nice project immediately:

def odd_by_sub(x):
    x = abs(x)
    while x > 1:
        x -= 2
    return bool(x)

for t in (-4, 5, 1, 0, -1, 98792743):
    print(t, odd_by_sub(t))

The newbie way with modulo:

´def odd_by_mod(x):
    if x % 2 == 0:
        return False
    else:
        return True

for t in (-4, 5, -5, 1, 0, -1, 98792743):
    print('%i odd: %s' % (t, odd_by_mod(t)))

The expert's way

def odd_by_and(x):
    return bool(x & 1)

for t in (-4, 5, 1, -5, 0, -1, 98792743):
    print(t, odd_by_and(t))

Another impractical (in Python especially without tail recursion removal) by recursion:

def odd_by_rec(x):
    if x < 0 :
        return odd_by_rec(-x)
    elif x < 2:
        return bool(x)
    else:
        return odd_by_rec(x-2)

# recursion in Python can not deal with too deep recursion so smaller values only
for t in (-4, 5, 1, 0, -1, 743):
    print(t, odd_by_rec(t))

What happens if you input -6? It looks to me as though your program will tell me it's odd.

Try thinking through your problem out loud. If you can tell yourself logically how it should get to the answer, you should be able to program it correctly.

For starters:
"If my number is positive, i can keep subtracting 2 from it without changing the answer, and will eventually get to 0 (if it was 2, 4, 6, ...) or -1 (if it was 1, 3, 5, ...)."

Now include negative numbers as well. Good luck!

print "input a number"
n = input()
while n < -3:
    n = n + 2

if n == 2 or n == -2 or n ==0:
    print "number is even"
    exit()
else:
    print "press enter"
    p = raw_input()

n = n - 2


if n == 2 or n == -2 or n == 0:
    print "number is even"
    i = raw_input() 
    exit()
else:
    print "press enter again"

l = raw_input()

while n != 2. and n >= -3:
    n = n - 2
if n <= -3: 
    print "number is odd"
elif n == 2 or n == -2:
    print "number is even"

g = raw_input()

thanx 4 the heads up

What happens if you input -6? It looks to me as though your program will tell me it's odd.

Try thinking through your problem out loud. If you can tell yourself logically how it should get to the answer, you should be able to program it correctly.

For starters:
"If my number is positive, i can keep subtracting 2 from it without changing the answer, and will eventually get to 0 (if it was 2, 4, 6, ...) or -1 (if it was 1, 3, 5, ...)."

Now include negative numbers as well. Good luck!

This question has already been answered. Start a new discussion instead.