[Tutor] A simple "if" and "elif" problem
alan.gauld at btinternet.com
Sat May 5 20:48:19 CEST 2012
On 05/05/12 19:01, Santosh Kumar wrote:
>>>> x = int(input("Please enter an integer: "))
> Please enter an integer: 42
> Now I want to add a filter in this script, I want when a user enter a
> string here it give a warning "Please enter a number like 0 or 2".
What happens if the user does not enter a valid integer
at the moment? You should get an error message?
That error message will provide the name of a particular type of Error.
Python lets you catch errors and handle them. You will find out how to
do that later in your tutorial.
Once you catch the error you will likely want to loop around and ask for
more input. You will need a loop - probably a while loop - to do that.
So in pseudo code you will wind up with something like:
read the input and convert to int
break out of the loop
except when there is an error:
print a message and go round the loop again
Author of the Learn to Program web site
More information about the Tutor