1

I have an 'if-elif-else' block and if a value is within that range it is assigned a certain value. However it when I run it just assigns it the value in the else block. This is my code:

if mile < 300:
    mileInfo['miles'] = 1
elif mile>=300 and mile <2000:
    mileInfo['miles'] = 2
elif mile>=2000 and mile <5000:
    mileInfo['miles'] = 3
else:
    mileInfo['miles'] = 4

Mile returns a float, but I thought that this didn't matter as much as in Java for example.

Thanks

user94628
  • 3,641
  • 17
  • 51
  • 88
  • 4
    What is ``mile``? What is ``mileInfo``? What is the output/error you get you don't expect? – Gareth Latty Nov 28 '12 at 19:37
  • 3
    As another note, Python supports 3-item comparisons, so you can do, for example, ``elif 300 <= mile < 2000:`` to simplify your code. That said, as you are in an ``elif``, it'll only run if the value is more than ``300`` anyway, so it's pointless to check again. – Gareth Latty Nov 28 '12 at 19:38
  • 1
    value is mile which is a float and it just assigns miles to value 4. – user94628 Nov 28 '12 at 19:39
  • 1
    I'm not asking what type, I'm saying give us a runnable example with values that produce the problem you experience. Give us a value that when we do ``miles = x`` it has the problem you get. – Gareth Latty Nov 28 '12 at 19:40
  • 1
    after the `else`, include `print mile, type(mile)`, and see why you always get catched by the `else` in the first place. Are you sure `mile` is not always larger than 5000? – heltonbiker Nov 28 '12 at 19:41
  • @heltonbiker Thanks for suggesting the type check. Mile is a str, I thought the result was a float. – user94628 Nov 28 '12 at 19:48

2 Answers2

7

Maybe mile is a string containing a number? It will not be automatically converted.

>>> "1" < 100
False
>>> "1" == 1
False

You don't need to have the elif re-check that the previous if was false. If the value wasn't < 300, it is guaranteed to be >=300.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
  • (+1) I like the way you're thinking – NPE Nov 28 '12 at 19:40
  • Thanks mile is a float returns a value eg. 325.0124. It returns the difference between two points. I initially had the code as less than 300 elif less than 2000 elif less than 5000. But that wasn't working either so I changed it to what I have now. – user94628 Nov 28 '12 at 19:44
  • 1
    @user94628 please do a 'print type(mile)' and post the result as a comment. – XORcist Nov 28 '12 at 19:48
  • Stuff like `"1" < 100` gives error in Python3. I suppose that's one of the things that they changed during the transition from Python2. – J...S Mar 10 '18 at 17:55
3

The issue was that 'mile' was a string and as pointed out by other members string is not automatically converted. So I changed code to:

mileInt = int(float(mile))

if mileInt < 300:
    mileInfo['miles'] = 1
elif mileInt < 2000:
    mileInfo['miles'] = 2
elif mileInt < 5000:
    mileInfo['miles'] = 3
else:
    mileInfo['miles'] = 4

Using print type(mile) helps check what the type is.

user94628
  • 3,641
  • 17
  • 51
  • 88
  • 1
    Actually, `mileInt = int(mile)` should be enough. A pity that you unaccepted the earlier correct answer to just reiterate that it was correct. – Has QUIT--Anony-Mousse Dec 01 '12 at 14:56
  • @Anony-Mousse you're right, suggesting to check mile type was the solution to correcting my original code. I've accepted your answer in recognition of this. Thanks again for your help. – user94628 Dec 02 '12 at 02:29