Python Forum
Functions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Functions (/thread-15443.html)



Functions - kbranch3791 - Jan-17-2019

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.


RE: Functions - ichabod801 - Jan-17-2019

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]