[Tutor] check_range
Brian van den Broek
bvande at po-box.mcgill.ca
Wed Dec 15 08:59:00 CET 2004
DogWalker said unto the world upon 2004-12-15 00:32:
> "Brian van den Broek" <bvande at po-box.mcgill.ca> said:
>
>>Marc Gartler said unto the world upon 2004-12-14 18:12:
>>
>>>Hi all,
>>>
>>>I am fairly new to both Python & programming, and am attempting to
>>>create a function that will test whether some user input is an integer
>>>between 10 and 89, but the check isn't happening...
>>>
>>>def check_range(myrange):
>>> if range(myrange) != range(10,89):
>>> return "False"
>>> else:
>>> return "True"
>>>
>>>...this gets called later via:
>>> if check_range(input):
>>> done = "True"
>>> return int(input)
>
> [...]
>
>>I have a some style suggestions for you, too.
>>
>>Try it this way:
>>
>>>>>def check_in_range(value):
>>
>> in_range = False
>> if 9 < value < 90:
>> in_range = True
>> return in_range
>
>
> Shorter:
> def check_in_range(value):
> return 9 < value < 90
Indeed. Good one. I never seem to think of such very direct ways, but
there you have it. It might say more about my psychology than anything
else, but I think I'd be tempted to put a brief comment on that code.
(Such as # returns True if the test, False otherwise.) My guess is that
if I had to read it more than twice, the time spent writing the comment
would be more than regained in spending less time scratching my head.
This might also be a matter of little experience, too.
At any rate, I certainly would agree that yours is bound to be better in
any situation where speed of execution mattered a lot.
Best,
Brian vdB
More information about the Tutor
mailing list