[Tutor] step value
Steven D'Aprano
steve at pearwood.info
Mon Jun 13 12:07:26 CEST 2011
Vincent Balmori wrote:
> I am stuck on a question for Absolute Beginner's. I googled this and there have
> been others who have not understood the question and I am also not clear on the
> question he is asking. This function is a part of a tic tac toe program."Improve
> the function ask_number() so that the function can be called with a step value.
> Make the default value of step 1."
It's hard to guess what the author was thinking if he doesn't explain
it, but I think what he means is this:
The aim of the "ask_number" function is to get the caller -- you -- to
enter a number in the range(low, high). That much is easy, and you
already have the function doing that.
But the range function can take a third, optional argument "step".
Instead of (for example)
range(1, 20) => [1, 2, 3, 4, 5, 6, ... 17, 18, 19]
you can pass a step N and get only every Nth value, e.g.:
range(1, 20, 3) => [1, 4, 7, 10, 13, 16, 19] # every third number
So, you have a function which calls range() internally:
> def ask_number(question, low, high):
> """Ask for a number within a range."""
> response = None
> while response not in range(low, high):
> response = int(input(question))
> return response
and your mission is to modify that function so that it takes an optional
argument that defaults to 1, and passes that argument to the range()
function as step.
Does that help?
--
Steven
More information about the Tutor
mailing list