Python for the absolute beginner

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • edperez7
    New Member
    • Nov 2013
    • 1

    Python for the absolute beginner

    I need help with this challenge I have in a book I'm reading. I can't really understand the questions.

    Improve the function ask_number()so that the function can be
    called with a step value. Make the default value of step 1.

    Here's the function
    Code:
    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
    Last edited by bvdet; Nov 3 '13, 04:24 PM. Reason: Edit #1: Please use [CODE] and [/CODE] tags when posting code or formatted data. Edit #2: Add indentation to the code.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Code:
    def ask_number(question, low, high, step=1):
        response = None
        while response not in range(low, high, step):
            response = int(input(question))
        return response
    
    if __name__ == "__main__":
        print ask_number("Enter an even number between 0 and 20", 0, 21, step=2)

    Comment

    Working...