[Tutor] Newbie question re. Functions
kent37 at tds.net
Tue Jan 31 12:12:38 CET 2006
Jon Moore wrote:
> I am still working my way through my 'Python for absolute beginners
> book' and have hit a brick wall with one of the end of chapter exercises.
> The challenge says:
> 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
> The author has not eluded to 'step values' in anyway that I can see in
> the proceeding chapters!
I have the book and I don't understand what he is asking for in that
To me a 'step value' would be something that alters a sequence, for
example the third argument to range() is a step value:
Help on built-in function range in module __builtin__:
range([start,] stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
>>> range(0, 6)
[0, 1, 2, 3, 4, 5]
>>> range(0, 6, 2)
[0, 2, 4]
More information about the Tutor