Python Forum

Full Version: Functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Not understanding the question. Improve the function ask_number() so that the function can be called with a step value. Make the default value of step 1.
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
First post ever! Hope I did this right.
The key is that range can take a third parameter called step:

>>> list(range(2, 10, 2))
[2, 4, 6, 8]
>>> list(range(2, 10, 3))
[2, 5, 8]