Problems with if/elif statement syntax

cokofreedom at gmail.com cokofreedom at gmail.com
Thu Nov 22 06:29:29 EST 2007


On Nov 22, 12:16 pm, oj <ojee... at gmail.com> wrote:
> On Nov 22, 11:09 am, Neil Webster <nswebs... at gmail.com> 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
>
> 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)



More information about the Python-list mailing list