[Tutor] Range of float value
Kent Johnson
kent37 at tds.net
Thu Feb 8 20:32:47 CET 2007
Johan Geldenhuys wrote:
> OK, this what I wanted:
>
> I have a value: a = 48.41
>
> My lowValue is: lowValue = 48.35
> My highValue is : highvalue = 48.45
>
> if a <= lowValue:
> print 'value below limit'
>
> if a >= highValue:
> print value above limit'
>
> I though that it could be possible to have a range between 48.35 and
> 48.45 that could have a step of 0.1
What is wrong with
def lookAtRange(a):
if lowValue <= a <= highValue:
print 'In limits'
..etc
??
>
> This works fine with normal intgers:
>
> *>>> def lookAtRange(a):
> ... if a in range(40, 110, 10): *#Would like to have: if a in
> range(48.35, 48.45, 0.1):
> *... print 'In limits'
> ... else:
> ... print 'Out of limits'
> ...
>> >> lookAtRange(40)
> In limits
>> >> lookAtRange(50)
> In limits
>> >>
>> >> lookAtRange(20)
> Out of limits
>> >>
>> >> lookAtRange(120)
> Out of limits
>> >> *
Are you sure this is what you want? Have you tried lookAtRange(45) for
example?
The range function creates a list of integers:
In [1]: range(40, 110, 10)
Out[1]: [40, 50, 60, 70, 80, 90, 100]
The 'in' operator tests for membership. 40 is in the list; 20 and 45 are
not.
I think even in the case of integers the range test with < is what you want.
Kent
More information about the Tutor
mailing list