Question
%Python help% For this exercise we're going to create a simple Car class. The car will keep track of its location, its orientation (north, south,
%Python help%
For this exercise we're going to create a simple Car class. The car will keep track of its location, its orientation (north, south, east, or west), and the amount of gas in the tank. The car will be able to drive forward or in reverse, turn left or right, and refill its gas tank. Script instructions Call your class Car. Write an __init__() method with self as the only parameter. This method should do the following: Set an attribute x (representing the car's x coordinate) to 0. Set an attribute y (representing the car's y coordinate) to 0. Set an attribute orient (representing the car's orientation) to "n" (for north). Set an attribute gas (representing the amount of gas in the car's tank) to 10.0. Write a drive() method with parameters self and reverse. reverse should take a Boolean value; give it a default value of False. This method should do the following: If the gas attribute is less than 0.2, raise a RuntimeError. This method should change the x or y attribute depending on the value of the orient attribute and the parameter reverse. When driving north, decrease y by 1; south, increase y by 1; west, decrease x by 1; east, increase x by 1. This method should decrease the gas attribute by 0.2. Write a turn() method with parameters self and dir (for direction). dir should be 'l' or 'r'. This method should do the following: Change the orient attribute depending on the value of dir. For example, if orient was 'n' and dir is 'r', orient should become 'e'. Write a refill() method with self as the only parameter. This method should do the following: Set the gas attribute to 10.0. Write a status() method with self as the only parameter. This method should do the following: Print a message indicating the car's current position and orientation and the amount of gas in the tank. Write docstrings for each method. (__init__() doesn't need a docstring.) Make an if __name__ == "__main__": statement in which you create an instance of your class and invoke each method (you don't have to explicitly invoke __init__())
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started