
Notice that commands that change the location or direction of the car have to update the class data members appropriately.
Notice also the use of print statements on the car object. This only works after you've defined __str__() for the class.
# Program header and description here.
import random
# Maybe other imports if you need them.
# Constants:
EAST = 0
NORTH = 90
...
class ToyCar:
def __init__( self, x = 0, ... ):
# Command should fail and print "ERROR: Illegal direction entered."
# if the provided direction is invalid.
...
def __str__( self ):
""" Generate a string containing information on
the class object. """
...
def turnLeft( self ):
...
# the other ToyCar methods
# The other functions you'll need to supply. You can have more, but
# must have at least these four.
def randomDrive( car, n ):
...
def goto( car, x, y ):
...
def gasStation():
...
def gasUp(car):
...
# You're welcome to add a main() function and a call to main() to
# include some test code. But you can also test it from the
# interactive loop. Both are illustrated below.
> python Python 3.6.9 (default, Nov 25 2022, 14:10:45) [GCC 8.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from Project2 import * >>> c1 = ToyCar( 100, -100, SOUTH ) >>> print( c1 ) Your car is at location (100, -100), heading South >>> c2 = ToyCar() >>> print( c2 ) Your car is at location (0, 0), heading East >>> c3 = ToyCar( y = -50, d = 313 ) ERROR: Illegal direction entered. >>> c3 = ToyCar( y = -50, d = 90 ) >>> print( c3 ) Your car is at location (0, -50), heading North >>> c = ToyCar( d = NORTH ) >>> print( c ) Your car is at location (0, 0), heading North >>> c.__x # attributes must be private Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'ToyCar' object has no attribute '__x' >>> c.forward( 100 ) DEBUG: moving forward 100 >>> print( c ) Your car is at location (0, 100), heading North >>> c.getDir() 90 >>> c.turnLeft() DEBUG: turning West >>> print( c ) Your car is at location (0, 100), heading West >>> c.forward( -50 ) ERROR: Illegal distance entered. >>> c.forward( 50 ) DEBUG: moving forward 50 >>> print( c ) Your car is at location (-50, 100), heading West >>> c.setDir( SOUTH ) DEBUG: setting direction South >>> print( c ) Your car is at location (-50, 100), heading South >>> ( c.getX(), c.getY() ) (-50, 100) >>> randomDrive( c, 5 ) # yours will be different DEBUG: turning West DEBUG: moving forward 93 DEBUG: turning North DEBUG: moving forward 88 DEBUG: turning West DEBUG: moving forward 100 DEBUG: turning South DEBUG: moving forward 34 DEBUG: turning East DEBUG: moving forward 38 >>> print( c ) Your car is at location (-205, 154), heading East >>> gasUp( c ) # yours will be different Located gas station at (91, 9) DEBUG: setting direction East DEBUG: moving forward 296 DEBUG: setting direction South DEBUG: moving forward 145 >>> print( c ) Your car is at location (91, 9), heading South >>> gasStation() # yours will different Located gas station at (-92, 42) (-92, 42) >>> print( c ) Your car is at location (91, 9), heading South >>> c4 = ToyCar(20, 30, SOUTH) >>> print(c4) Your car is at location (20, 30), heading South >>> goto( c4, -50, -25 ) DEBUG: setting direction West DEBUG: moving forward 70 DEBUG: setting direction South DEBUG: moving forward 55 >>> print(c4) Your car is at location (-50, -25), heading South >>>
Your output for these same commands would be slightly different, because the commands randomDrive, gasStation, and gasUp involved randomly generated values.
The above shows running the program interactively in the Python loop. You could also add a main function in your Project2.py file that includes the same commands. This is a nice way to debug without having to enter all the commands in the interactive loop. That would look as follows:
def main():
c1 = ToyCar( 100, -100, SOUTH )
print( c1 )
c2 = ToyCar()
print( c2 )
c3 = ToyCar( y = -50, d = 313 )
c3 = ToyCar( y = -50, d = 90 )
print( c3 )
c = ToyCar( d = NORTH )
print( c )
c.forward( 100 )
print( c )
# This one needs a print, or you won't see it.
print( c.getDir() )
c.turnLeft()
print( c )
c.forward( -50 )
c.forward( 50 )
print( c )
c.setDir( SOUTH )
print( c )
# This one needs a print, or you won't see it.
print( c.getX(), c.getY() )
randomDrive( c, 5 )
print( c )
gasUp( c )
print( c )
gasStation()
print( c )
c4 = ToyCar(20, 30, SOUTH)
print(c4)
goto( c4, -50, -25 )
print(c4)
main()
You would then call the program like any other. You also might want
to add additional prints to make the output more understandable.
Please comment out the call to main() before you submit.
Your file must compile and run before submission. It must also contain a header with the following format:
# File: Project2.py # Student: # UT EID: # Course Name: CS303E # # Date: # Description of Program:
Use functions: If you see that you're doing some computation multiple times (e.g., getting the name of a direction like "East" from the number 0) write a function to handle that. Always test the function before you move on.
Use print for debugging: If you don't quite see what's going wrong at any point, add print statements. Often seeing what values are being generated as you go is enough to understand your error. Just remember to comment out or remove the extra print statement before you submit.