[Tutor] check_range

Jeff Shannon jeff at ccvcorp.com
Wed Dec 15 00:58:38 CET 2004


R. Alan Monroe wrote:
>>def check_range(myrange):
>>        if range(myrange) != range(10,89):
>>                return "False"
>>        else:
>>                return "True"
> 
> 
> For this to work out, the user's input would have to be a giant string
> containing 10, 11, 12, 13, etc.

Not quite, actually.

Presuming that myrange is an integer, range(myrange) will generate a 
list of integers starting with 0 and continuing up to (but not 
including) myrange, while range(10,89) generates a list of integers 
starting with 10 and continuing up to (but not including) 89.  These 
lists can never be equal, because range(10,89) will never include the 
integers 0-9 but range(myrange) will (unless myrange is less than 10).

> Unless I mistunderstood your requirements, what you're probably looking for is:
> 
> if myrange in range(10,90):  # "in" is the key word here
>     return True
> else
>     return False

This is, however, the correct solution. :)  Presuming again, of 
course, that myrange is an integer -- but be aware that user input 
normally comes in the form of strings, so it will be necessary, at 
some point, to create an integer from that string using int().

Jeff Shannon
Technician/Programmer
Credit International



More information about the Tutor mailing list