O'Reilly logo

Sage Beginner's Guide by Craig Finch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - raising and handling exceptions

Let's go back to the combat simulation example. We wrote code that printed an error message when the move method received invalid arguments. We will rewrite the error-handling code to use exceptions. Replace the contents of the file vehicle.py in the combatsim package with the following code:

 import sage.all class Ground_Vehicle(): """Base class for all ground vehicles""" def __init__(self, position): """Create a vehicle instance position (x,y) tuple of coordinates """ # Intialize position self._x = position[0] self._y = position[1] def move(self, direction, distance): """Move the vehicle. Arguments: direction floating-point number representing the compass angle of movement in degrees. North is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required