[Tutor] Newbie question re. Functions
Ed Singleton
singletoned at gmail.com
Wed Feb 1 15:14:19 CET 2006
On 31/01/06, Jon Moore <jonathan.r.moore at gmail.com> wrote:
> Improve the function ask_number() so that the function can be called with a
> step value. Make the default value of step 1.
>
> The function looks like this:
>
> def ask_number(question, low, high):
> """Ask for a number within the range"""
> response = None
> while response not in range(low, high):
> response = int(raw_input(question))
> return response
To be honest, this made sense to me. I assumed the author wants you
to be able to do the following:
ask_number("Give me an even number between 1 and 10", 1, 10, 2)
The solution would be:
def ask_number(question, low, high, step=1):
"""Ask for a number within the range"""
response = None
while response not in range(low, high, step):
response = int(raw_input(question))
return response
But I definitely agree that he said it very, very badly.
Ed
More information about the Tutor
mailing list