By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,705 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,705 IT Pros & Developers. It's quick & easy.

Python for the absolute beginner

P: 1
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
Expand|Select|Wrap|Line Numbers
  1. def ask_number(question, low, high):
  2.     """Ask for a number within a range."""
  3.     response = None
  4.     while response not in range(low, high):
  5.         response = int(input(question))
  6.     return response
Nov 1 '13 #1
Share this Question
Share on Google+
1 Reply

bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. def ask_number(question, low, high, step=1):
  2.     response = None
  3.     while response not in range(low, high, step):
  4.         response = int(input(question))
  5.     return response
  6.  
  7. if __name__ == "__main__":
  8.     print ask_number("Enter an even number between 0 and 20", 0, 21, step=2)
Nov 3 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.