Dealing with the imperfect (or how to handle errors)
Next: The End Up: Non-Programmers Tutorial For Python Previous: File IO Contents
Subsections
So you now have the perfect program, it runs flawlessly, except for one detail, it will crash on invalid user input. Have no fear, for Python has a special control structure for you. It's called try
and it tries to do something. Here is an example of a program with a problem:
print "Type Control C or -1 to exit" number = 1 while number != -1: number = int(raw_input("Enter a number: ")) print "You entered: ",number
Notice how when you enter @#&
it outputs something like:
Traceback (innermost last): File "try_less.py", line 4, in ? number = int(raw_input("Enter a number: ")) ValueError: invalid literal for int(): @#&
As you can see the int
function is unhappy with the number @#&
(as well it should be). The last line shows what the problem is; Python found a ValueError
. How can our program deal with this? What we do is first: put the place where the errors occurs in a try
block, and second: tell Python how we want ValueError
s handled. The following program does this:
print "Type Control C or -1 to exit" number = 1 while number != -1: try: number = int(raw_input("Enter a number: ")) print "You entered: ",number except ValueError: print "That was not a number."
Now when we run the new program and give it @#&
it tells us ``That was not a number.'' and continues with what it was doing before.
When your program keeps having some error that you know how to handle, put code in a try
block, and put the way to handle the error in the except
block.
Exercises
Update at least the phone numbers program so it doesn't crash if a user doesn't enter any data at the menu.
Next: The End Up: Non-Programmers Tutorial For Python Previous: File IO Contents Josh Cogliati jjc@honors.montana.edu Wikibooks Version: Wikibooks Non-programmers Python Tutorial Contents