Notice that commands that change the location or direction of the car must update the class data members appropriately to represent the new state.
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. # Any imports you need. # 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. You can assume x and # y are integers. ... 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 this one: def goto( car, x, y ): ... # 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.8.10 (default, Feb 4 2025, 15:02:54) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from ToyCar 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 = 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.getDir() 90 >>> c.setDir( 45 ) ERROR: Illegal direction entered. >>> print( c ) Your car is at location (0, 0), heading North >>> c.forward( 100 ) DEBUG: moving forward 100 >>> print( c ) Your car is at location (0, 100), heading North >>> 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.turnRight() DEBUG: turning West >>> print( c ) Your car is at location (-50, 100), heading West >>> c.forward( 25 ) DEBUG: moving forward 25 >>> print( c ) Your car is at location (-75, 100), heading West >>> goto( c, 0, 0 ) DEBUG: setting direction East DEBUG: moving forward 75 DEBUG: setting direction South DEBUG: moving forward 100 >>> print( c ) Your car is at location (0, 0), heading South >>> goto( c, 100, 100 ) DEBUG: setting direction East DEBUG: moving forward 100 DEBUG: setting direction North DEBUG: moving forward 100 >>> print( c ) Your car is at location (100, 100), heading North >>> c.getX() 100 >>> c.getY() 100 >>>
The above shows running the program interactively in the Python loop. You could also add a main function in your ToyCar.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 ) # Create car c1 print( c1 ) # and show its state c2 = ToyCar() # create car c2 print( c2 ) # and show its state c3 = ToyCar( y = -50, d = 90 ) # create car c3 print( c3 ) c = ToyCar( d = NORTH ) # create car c print( c ) print( c.getDir() ) # where is c headed? c.setDir( 45 ) # this should fail print( c ) c.forward( 100 ) # move c forward 100 print( c ) c.turnLeft() # turn c left print( c ) c.forward( -50 ) # this should fail c.forward( 50 ) # move c forward 50 print( c ) c.setDir( SOUTH ) # turn c toward the south print( c ) c.turnRight() # turn c right (West) print( c ) c.forward( 25 ) # move c forward 25 print( c ) goto( c, 0, 0 ) # call your external function print( c ) # to go to (0, 0) goto( c, 100, 100 ) # now goto (100, 100) print( c ) print( c.getX() ) # get the current X print( c.getY() ) # get the current Y main()You would then call the program like any other. 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: ToyCar.py # Student: # UT EID: # Course Name: CS303E # # Date: # Description of Program:
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.
Use Functions: Functions provide a powerful mechanism for abstraction. Once you've defined a function, it's as if you had an additional primitive in the programming language. For example, once we defined isPrime(n), you can treat that as it it were native to the language. Henceforth, whenever you need to check whether an integer is prime, you only need import that function from the appropriate library and call it. As long as you trust whomever programmed it, you really don't need to worry about how it works.
If you see that you're doing some computation multiple times write a function to handle that. Always test the function before you move on. Also, make your functions as robust as possible. For example, our isPrime function works for arbitrary integers, but will crash for other inputs. For a production version, it would be better to validate the arguments more thoroughly.
Functions provide all of the following benefits: