Book Image

Sage Beginner's Guide

By : Craig Finch
1 (1)
Book Image

Sage Beginner's Guide

1 (1)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 0, 
                    east is 90, south is 180, and west is 270. 
                    0 <= direction &lt...