473,410 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

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
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
1 1518
bvdet
2,851 Expert Mod 2GB
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

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

Similar topics

0
by: Irmen de Jong | last post by:
QOTW: "What can I do with Python that I can't do with C#? You can go home on time at the end of the day." -- Daniel Klein "Python lends itself to playing with it and to discussing the merits of...
3
by: Emile van Sebille | last post by:
QOTW: "...I want to reserve .var for the "with" statement (a la VB)." --Guido van Rossum "...at some point in the future Python *will* grow (optional) type declarations for all sorts of things...
0
by: Emile van Sebille | last post by:
QOTW: "I've tried many ways over the years to represent equations legibly using Python and my experience that case sensitivity, while certainly no panacea, helps." -- Tim Hochberg "What's your...
0
by: Emile van Sebille | last post by:
QOTW: "Oddly enough it is in Python that I have had the most fun programming. It is in Python that I find myself not only the most expressive but the most elegant in my programming. In Python my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.