GitHub Repository Submission

When using GitHub mode, paste your repository URL below and click Save URL to store it. The saved URL will be automatically included with every message you send until you choose to clear it. Learn more

Your GitHub repository URL is saved. LSBot will automatically fetch your latest code from this repository for each message. To change the URL, clear it first and save a new one.

Exercise 1

Create a Car class that meets these requirements:

  • Each Car object should have a model, model year, and color provided at instantiation time.
  • You should have an instance variable that keeps track of the current speed. Initialize it to 0 when you instantiate a new car.
  • Create instance methods that let you turn the engine on, accelerate, brake, and turn the engine off. Each method should display an appropriate message.
  • Create a method that prints a message about the car's current speed.
  • Write some code to test the methods.

Solution

class Car:

    def __init__(self, model, year, color):
        self.model = model
        self.year = year
        self.color = color
        self.speed = 0

    def engine_start(self):
        print('The engine is on!')

    def engine_off(self):
        self.speed = 0
        print("Let's park this baby!")
        print('The engine is off!')

    def speed_up(self, number):
        self.speed += number
        print(f'You accelerated {number} mph.')

    def brake(self, number):
        self.speed -= number
        print(f'You decelerated {number} mph.')

    def get_speed(self):
        print(f'Your speed is {self.speed} mph.')

lumina = Car('chevy lumina', 1997, 'white')
lumina.engine_start() # The engine is on!
lumina.get_speed()    # Your speed is 0 mph.
lumina.speed_up(20)   # You accelerated 20 mph.
lumina.get_speed()    # Your speed is 20 mph.
lumina.speed_up(30)   # You accelerated 30 mph.
lumina.get_speed()    # Your speed is 50 mph.
lumina.brake(15)      # You decelerated 15 mph.
lumina.get_speed()    # Your speed is 35 mph.
lumina.brake(30)      # You decelerated 30 mph.
lumina.get_speed()    # Your speed is 5 mph.
lumina.engine_off()   # Let's park this baby!
                      # The engine is off
lumina.get_speed()    # Your speed is 0 mph.

Hi! I'm LSBot. I can help you think through the selected exercise by giving you hints and guidance without revealing the solution. Your code from the editor will be automatically detected. Want to know more? Refer to the LSBot User Guide .

Detected solution
Loading...

Submit your solution for LSBot review.

Hi! I'm LSBot. Your code from the editor will be automatically detected. I'll review your solution and provide feedback to help you improve. Ask questions about your solution or request a comprehensive code review. Want to know more? Refer to the LSBot User Guide .

Detected solution
Loading...