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"
```

sandman64
0
Light Poster

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
by vegaseat because:
*
added code tags *

Jump to Post

elif n < 2:

Jump to PostWell, for God sake gives the error message you get!

Jump to PostAre 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.

Jump to PostWhat 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.

vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague

**elif n < 2:**

sandman64
0
Light Poster

i did that but its still giving me a syntax error

elif n < 2:

vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague

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

sandman64
0
Light Poster

"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!

TrustyTony
888
pyMod
Team Colleague
Featured Poster

Edited
by TrustyTony because:
*
n/a *

sandman64
0
Light Poster

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()
```

TrustyTony
888
pyMod
Team Colleague
Featured Poster

sandman64
0
Light Poster

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

TrustyTony
888
pyMod
Team Colleague
Featured Poster

sandman64
0
Light Poster

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.

TrustyTony
888
pyMod
Team Colleague
Featured Poster

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
by TrustyTony because:
*
n/a *

sandman64
0
Light Poster

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))`

raptr_dflo
48
Posting Pro

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!

sandman64
0
Light Poster

```
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!

Swathi_4
0
Newbie Poster

elif n < 2:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.

Broken Link

**You're trying to visit a URL that doesn't currently exist on the web.**
Most likely, a member posted a link a long time ago to a web page that has since been removed.
It's also possible that there was a typo when posting the URL.
We redirect you to this notice instead of stripping out the link to preserve the integrity of the post.