Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Functions
#1
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.
Reply
#2
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]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020