Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: if error raised I want to stay in script

Tags:

python

I am doing some practice problems from online with python and I have a question about how to stay in a script if an error is raised. For example, I want to read in values from prompt and compare them to a set integer value inside the script. The only problem is that when someone enters something other than a number 'int(value)' (ex. value = 'fs') raises an error and exits the script. I want to have it so if this happens I stay inside the script and ask for another value to be entered at the prompt.

like image 362
acmisiti Avatar asked Jun 17 '11 00:06

acmisiti


Video Answer


1 Answers

success = false
while not success:
    try:
        value = raw_input('please enter an integer')
        int(value)
        success = true
    except:
        pass
like image 54
Peter Long Avatar answered Sep 20 '22 03:09

Peter Long