473,387 Members | 1,536 Online

# 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 1497
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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 0 by: EAS | last post by: Whenever I open a python program using a normal console window, it says: 'import site' failed; use -v for traceback What does this mean and how can I get rid of it? Also, I have this one program... 2 by: Brian van den Broek | last post by: Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a... 14 by: John Salerno | last post by: Here's an exercise I was doing to guess a number from 1-100. Just for fun (ha ha) I decided to add some error checking too, and now when I run it, the DOS prompt flashes real quick and disappears.... 10 by: connyledin | last post by: Im trying to create a version of the game Wumpus. Mine is called Belzebub. But im STUCK! And its due tuesday 2 maj. Im panicing! Can some one help me?? here is the file:... 13 by: Jim | last post by: Could somebody tell me why I need the "elif char == '\n'" in the following code? This is required in order the pick up lines with just spaces in them. Why doesn't the "else:" statement pick this... 4 by: huzzaa | last post by: I am using a switch statement that will decide from a random number what message to display. I want the random number to be between 0 and 100 and then if the number is say between 1 and 10 to... 2 by: juan-manuel.behrendt | last post by: Hello together, I wrote a script for the engineering software abaqus/CAE. It worked well until I implemented a selection in order to variate the variable "lGwU" through an if elif, else... 5 by: SpennH | last post by: I have had some basic knowledge of java through flash 8. I also use a freeware 3d software called blender. And it uses python for its game engine. So I have decided to pick up some python. This is... 2 by: Kid Programmer | last post by: Hello guys. I was wondering if you could use a thing in python in java. That is an elif statement. It is saying else if. It will execute a block of code if the condition in the if statement is... 0 by: Charles Arthur | last post by: How do i turn on java script on a villaon, callus and itel keypad mobile phone 0 by: aa123db | last post by: Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function \$name\$ (\$parameters\$) { } ... 0 by: emmanuelkatto | last post by: Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel 0 by: BarryA | last post by: What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress... 1 by: Sonnysonu | last post by: This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to... 0 by: Hystou | last post by: There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2... 0 by: marktang | last post by: ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,... 0 by: Hystou | last post by: Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can... 0 by: jinu1996 | last post by: In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.