E
EAS
How do you make a loop where the program keeps asking for
an integer or float until it gets one? (No letters or other input.)
an integer or float until it gets one? (No letters or other input.)
EAS said:How do you make a loop where the program keeps asking for
an integer or float until it gets one? (No letters or other input.)
Jeff said:Here's a function that can do the job.
def typed_input(prompt="", convert=int, catch=ValueError,
eprompt="Invalid input."):
while 1:
s = raw_input(prompt)
try:
return convert(s)
except catch:
print eprompt
You can send functions (int, float) and exceptions as parameters?
Amazing.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.