471,337 Members | 1,078 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Problems with if/elif statement syntax

Hi all,

I'm sure I'm doing something wrong but after lots of searching and
reading I can't work it out and was wondering if anybody can help?

I've got the following block of code:
if a >= 20 and a < 100:
if c == "c":
radius = 500
else:
radius = 250
elif (a >= 100) and (a < 500):
radius = 500
elif (a >= 500) and (a < 1000):
radius = 1000
elif (a >= 1000) and (a < 3000):
radius = 1500
elif (a >= 3000) and (a < 5000):
radius = 2000
else:
radius = 4000

No matter what value goes in for 'a' the radius always comes out as
4000.

What am I doing wrong?

Cheers

Neil
Nov 22 '07 #1
6 1382
oj
On Nov 22, 11:09 am, Neil Webster <nswebs...@gmail.comwrote:
Hi all,

I'm sure I'm doing something wrong but after lots of searching and
reading I can't work it out and was wondering if anybody can help?

I've got the following block of code:
if a >= 20 and a < 100:
if c == "c":
radius = 500
else:
radius = 250
elif (a >= 100) and (a < 500):
radius = 500
elif (a >= 500) and (a < 1000):
radius = 1000
elif (a >= 1000) and (a < 3000):
radius = 1500
elif (a >= 3000) and (a < 5000):
radius = 2000
else:
radius = 4000

No matter what value goes in for 'a' the radius always comes out as
4000.

What am I doing wrong?

Cheers

Neil
How is 'a' getting set?

My first thought, is that a is for some reason a string, instead of a
number, and the comparisons aren't doing what you expect.
>>a = "10"
a < 1000
False

If a is coming from user input, or from a web request or something,
make sure it's the correct type.

-Oliver.
Nov 22 '07 #2
On Nov 22, 12:16 pm, oj <ojee...@gmail.comwrote:
On Nov 22, 11:09 am, Neil Webster <nswebs...@gmail.comwrote:
Hi all,
I'm sure I'm doing something wrong but after lots of searching and
reading I can't work it out and was wondering if anybody can help?
I've got the following block of code:
if a >= 20 and a < 100:
if c == "c":
radius = 500
else:
radius = 250
elif (a >= 100) and (a < 500):
radius = 500
elif (a >= 500) and (a < 1000):
radius = 1000
elif (a >= 1000) and (a < 3000):
radius = 1500
elif (a >= 3000) and (a < 5000):
radius = 2000
else:
radius = 4000
No matter what value goes in for 'a' the radius always comes out as
4000.
What am I doing wrong?
Cheers
Neil

How is 'a' getting set?

My first thought, is that a is for some reason a string, instead of a
number, and the comparisons aren't doing what you expect.
>a = "10"
a < 1000

False

If a is coming from user input, or from a web request or something,
make sure it's the correct type.

-Oliver.
I would also look to write them this way
if 20 <= a < 100:
# do something

But you should ensure A is an integer / float, you can do this by
running;

print type(a)
Nov 22 '07 #3
On 22 Nov, 12:09, Neil Webster <nswebs...@gmail.comwrote:
Hi all,

I'm sure I'm doing something wrong but after lots of searching and
reading I can't work it out and was wondering if anybody can help?

I've got the following block of code:
if a >= 20 and a < 100:
if c == "c":
radius = 500
else:
radius = 250
elif (a >= 100) and (a < 500):
radius = 500
elif (a >= 500) and (a < 1000):
radius = 1000
elif (a >= 1000) and (a < 3000):
radius = 1500
elif (a >= 3000) and (a < 5000):
radius = 2000
else:
radius = 4000

No matter what value goes in for 'a' the radius always comes out as
4000.

What am I doing wrong?

Cheers

Neil
as Oliver pointed out, check if you're not compairing "a" as a string

I wanted to let you know that you can write the above conditions in a
more natural way, using the a<x<b idiom

e.g.

x=int(raw_input("write a number"))
if 5<=x<30:
print 'x is between 5 and 30'
Nov 22 '07 #4
On Nov 22, 12:33 pm, "riqu...@gmail.com" <riqu...@gmail.comwrote:
On 22 Nov, 12:09, Neil Webster <nswebs...@gmail.comwrote:
Hi all,
I'm sure I'm doing something wrong but after lots of searching and
reading I can't work it out and was wondering if anybody can help?
I've got the following block of code:
if a >= 20 and a < 100:
if c == "c":
radius = 500
else:
radius = 250
elif (a >= 100) and (a < 500):
radius = 500
elif (a >= 500) and (a < 1000):
radius = 1000
elif (a >= 1000) and (a < 3000):
radius = 1500
elif (a >= 3000) and (a < 5000):
radius = 2000
else:
radius = 4000
No matter what value goes in for 'a' the radius always comes out as
4000.
What am I doing wrong?
Cheers
Neil

as Oliver pointed out, check if you're not compairing "a" as a string

I wanted to let you know that you can write the above conditions in a
more natural way, using the a<x<b idiom

e.g.

x=int(raw_input("write a number"))
if 5<=x<30:
print 'x is between 5 and 30'
Argh, I really dislike raw_input. Though it helps to remind me to use
Try:Except blocks a lot.
Nov 22 '07 #5
Neil Webster wrote:
Hi all,

I'm sure I'm doing something wrong but after lots of searching and
reading I can't work it out and was wondering if anybody can help?

I've got the following block of code:
if a >= 20 and a < 100:
if c == "c":
radius = 500
else:
radius = 250
elif (a >= 100) and (a < 500):
radius = 500
elif (a >= 500) and (a < 1000):
radius = 1000
elif (a >= 1000) and (a < 3000):
radius = 1500
elif (a >= 3000) and (a < 5000):
radius = 2000
else:
radius = 4000

No matter what value goes in for 'a' the radius always comes out as
4000.

What am I doing wrong?

Cheers

Neil
You might try something like:

BOUNDS = [(500, 500), (1000, 1000),
(3000, 1500), (5000, 2000), (0, 4000)]
if a < 20:
raise ValueError('Too tiny: %r' % a)
if a < 100:
if c == "c":
radius = 500
else:
radius = 250
else:
for limit, radius in bounds:
if a < limit:
break
on the theory that it makes it easier to see what you
are driving the number towards. I'd even add an upper bound
check myself, so I could see other garbage coming in.

-Scott
Nov 22 '07 #6
co*********@gmail.com wrote:
On Nov 22, 12:33 pm, "riqu...@gmail.com" <riqu...@gmail.comwrote:
>On 22 Nov, 12:09, Neil Webster <nswebs...@gmail.comwrote:
>>Hi all,
I'm sure I'm doing something wrong but after lots of searching and
reading I can't work it out and was wondering if anybody can help?
I've got the following block of code:
if a >= 20 and a < 100:
if c == "c":
radius = 500
else:
radius = 250
elif (a >= 100) and (a < 500):
radius = 500
elif (a >= 500) and (a < 1000):
radius = 1000
elif (a >= 1000) and (a < 3000):
radius = 1500
elif (a >= 3000) and (a < 5000):
radius = 2000
else:
radius = 4000
No matter what value goes in for 'a' the radius always comes out as
4000.
What am I doing wrong?
Cheers
Neil
as Oliver pointed out, check if you're not compairing "a" as a string

I wanted to let you know that you can write the above conditions in a
more natural way, using the a<x<b idiom

e.g.

x=int(raw_input("write a number"))
if 5<=x<30:
print 'x is between 5 and 30'

Argh, I really dislike raw_input. Though it helps to remind me to use
Try:Except blocks a lot.

Hasn't anyone TRIED the code? I did, with a = 30 and c = 'x' radius
comes out as 250. So it seems the problem is somewhere else and not in
this bit of code.

Nov 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by EAS | last post: by
2 posts views Thread by Brian van den Broek | last post: by
14 posts views Thread by John Salerno | last post: by
4 posts views Thread by huzzaa | last post: by
2 posts views Thread by juan-manuel.behrendt | last post: by
5 posts views Thread by SpennH | last post: by
2 posts views Thread by Kid Programmer | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.