Learning Python 3 with the Linkbot/Dealing with the imperfect

closing files with with
We use the "with" statement to open and close files.

If some sort of error happens anywhere in this code (one of the files is inaccessible, the parse function chokes on corrupt data, etc.) the "with" statements guarantee that all the files will eventually be properly closed. Closing a file just means that the file is "cleaned up" and "released" by our program so that it can be used in another program.

catching errors with try
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  and it tries to do something. Here is an example of a program with a problem:

Notice how when you enter  it outputs something like:

Traceback (most recent call last): File "try_less.py", line 4, in    number = int(input("Enter a number: ")) ValueError: invalid literal for int with base 10: '\\@#&'

As you can see the  function is unhappy with the number   (as well it should be). The last line shows what the problem is; Python found a. How can our program deal with this? What we do is first: put the place where errors may occur in a  block, and second: tell Python how we want  s handled. The following program does this:

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  block, and put the way to handle the error in the   block.

Generating Errors: Controlling a Linkbot's Speed
We've seen in previous examples that we can write a function that makes a wheeled robot travel a certain distance. We can also control the rotational velocity of the motors with the  function. The  function expects a rotational speed with units of degrees/sec, but it would be nice if we could have a function where we could set the robot speed using inches/sec. The math equation to convert $$v$$ inches/sec to $$\omega$$ degrees/sec is

$$ \omega\mathrm{(deg/sec)} = \frac{v\mathrm{(inches/sec)}}{r\mathrm{(inches)}} * \frac{180\mathrm{(degrees)}}{\pi\mathrm{(radians)}} $$

where $$r$$ is the wheel radius. Lets expand our example from the ../Defining Functions section:

This example is all well and good. We define a new function  that sets the speed of a Linkbot wheeled vehicle and we use it to set the speed to 2.5 inches per second.

What if the programmer tries to set the speed to 1,000 inches/second? Or 1,000,000 inches/second? Although it would be cool to see a Linkbot compete with a Formula One race car, there are physical limitations that prevent the Linkbot's motors from moving more than 200 degrees/second. If the speed is too high, we should set an error the user can see and possibly deal with. This is called "raising an exception". The code to raise an exception looks like this:

When an exception is raised, the function immediately returns with the exception. These raised exceptions can be caught by try/except blocks. If the exception occurred outside of a try/except block, the entire program will quit and display the error message of the exception. In the  function, this means that if the   is executed, the two   statements will be skipped.

When I run the new program and I try to set the speed to 1000 inches a second, I get this output:

Traceback (most recent call last): File "./linkbot_speed.py", line 20, in    setSpeed(myLinkbot, 1000)     # Sets the speed to 1000 inches/sec File "./linkbot_speed.py", line 16, in setSpeed raise Exception('The speed is too high!') Exception: The speed is too high!

Now you can use try/catch blocks to deal with possible errors. Lets try writing a program that tries to set the speed to 10 inches per second again, except every time it encounters an exception, in reduces the requested speed by 1 inch/second and tries again.

The output is Trying to set speed to: 10inches/sec Failed. Trying to set speed to: 9inches/sec Failed. Trying to set speed to: 8inches/sec Failed. Trying to set speed to: 7inches/sec Failed. Trying to set speed to: 6inches/sec Success!

Lets step through this program together to make sure we fully understand what is happening.


 * # 1 : When we first get to this line, we create a new variable called  and set its value to "10".
 * # 2 : Enter an infinite loop
 * # 3 : Try to set the speed.  is currently 10, which is too high. The   function raises an exception. Since we are in a try/except block, we immediately go to the except block since an exception was thrown. Proceed to # 5
 * # 5 : Decrease  by one.   is now 9. This is the end of our   loop, which means that Python goes back to the beginning of the loop.
 * # 3 : We end up at # 3 again, except  is now 9. Still too high, exception is thrown.
 * # 5 : Again, we decrease  to 8.
 * # 3 : Still too high...
 * # 5 : Reduce to 7...
 * # 3 : Still too high...
 * # 5 : Reduce to 6.
 * # 3 : Now it succeeds. Since it succeeded, no exception was raised. Continue to # 4
 * # 4 : This  statement pops us out of the loop. Proceed to # 6 and the rest of the program.

Exercises
Update at least the phone numbers program (in section Dictionaries) so it doesn't crash if a user doesn't enter any data at the menu.