range variable not detecting floating points numeric values inside the given range

Question:

the code prints 9 even thought 6.33 falls into the range in the 3rd if statement not zero but within the range (5,8)

i was expecting it to print 3
the code

address="xyz"
size="6.33"
if address == 0 and float(size) in range(1, 5):
    print("1")
elif address == 0 and float(size) in range(5, 8):          
    print("2")
elif address != 0 and float(size) in range(5, 8):         
    print("3")
elif address == 0 and float(size) in range(8, 10):        
    print("4")
elif address != 0 and float(size) in range(10, 19):         
    print("5")
elif address == 0 and float(size) in range(10, 19):          
    print("6")
elif address != 0 and float(size) in range(20, 100):         
    print("7")
elif address != 0 and float(size) in range(20, 100):   
    print("8")
else:           
    print("9")

how do i fix the code so that it is able to detect floating point values in the given range

Asked By: Bilal

||

Answers:

range generates only integers. For example, range(2,6) will generate (2,3,4,5).

Your problem can be solved by:

address="xyz"
size="6.33"
if address == 0 and float(size)>=1 and  float(size)<5:
    print("1")
elif address == 0 and float(size)>=5 and  float(size)<8:          
    print("2")
elif address != 0 and float(size)>=5 and  float(size)<8:         
    print("3")
elif address == 0 and float(size)>=8 and  float(size)<10:        
    print("4")
elif address != 0 and float(size)>=10 and  float(size)<19:         
    print("5")
elif address == 0 and float(size)>=10 and  float(size)<19:          
    print("6")
elif address != 0 and float(size)>=20 and  float(size)<100:         
    print("7")
elif address == 0 and float(size)>=20 and  float(size)<100:   
    print("8")
else:           
    print("9")

Or more efficiently, using nested conditions,

address="xyz"
size="6.33"
if address == 0:
    if float(size)>=1 and  float(size)<5:
        print("1")
    elif float(size)>=5 and float(size)<8:          
        print("2")
    elif float(size)>=8 and float(size)<10:          
        print("4")
    elif float(size)>=10 and float(size)<19:          
        print("6")
    elif float(size)>=20 and float(size)<100:          
        print("8")
    else:
        print("9")
else:
    if float(size)>=5 and float(size)<8:          
        print("3")
    elif float(size)>=10 and float(size)<19:          
        print("5")
    elif float(size)>=20 and float(size)<100:          
        print("7")
    else:
        print("9")

Answered By: rajkumar_data

All right, here there is more to unpack:

range(3) is more like [0, 1, 2], i.e. sequence of integers, not an interval of real values. So:

>>> 1 in range(3)
True
>>> 1.5 in range(3)
False

But it is also true that

>>> 1.0 in range(3)

because x in s is True if an item of s is equal to x, else False and also:

>>> 1 == 1.0
True

See official documentation on Common Sequence Operations: https://docs.python.org/3/library/stdtypes.html#typesseq-common

But generally using equality and floating point numbers is a bad idea as it can lead to surprising results due to binary representation of floating point numbers:

>>> 0.3 + 0.3 == 0.6
True
>>> 0.3 + 0.3 + 0.3 == 0.9
False
>>> 10 * (0.3 + 0.3 + 0.3) == 9
True

One reasonable way how to fix your code is:

address = "xyz"
size = float("6.33")  # single conversion here
if address == 0 and 1 <= size < 5:
    print("1")
# ...
elif address != 0 and 20 <= size < 100:   
    print("8")
else:           
    print("9")

Chained comparison (e.g. 1 < 2 < 3) is described here: https://docs.python.org/3/library/stdtypes.html#comparisons

Answered By: Roman Pavelka
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.