Keine Beschreibung

car.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435
  1. """A class that can be used to represent a car."""
  2. class Car():
  3. """A simple attempt to represent a car."""
  4. def __init__(self, manufacturer, model, year):
  5. """Initialize attributes to describe a car."""
  6. self.manufacturer = manufacturer
  7. self.model = model
  8. self.year = year
  9. self.odometer_reading = 0
  10. def get_descriptive_name(self):
  11. """Return a neatly formatted descriptive name."""
  12. long_name = str(self.year) + ' ' + self.manufacturer + ' ' + self.model
  13. return long_name.title()
  14. def read_odometer(self):
  15. """Print a statement showing the car's mileage."""
  16. print("This car has " + str(self.odometer_reading) + " miles on it.")
  17. def update_odometer(self, mileage):
  18. """
  19. Set the odometer reading to the given value.
  20. Reject the change if it attempts to roll the odometer back.
  21. """
  22. if mileage >= self.odometer_reading:
  23. self.odometer_reading = mileage
  24. else:
  25. print("You can't roll back an odometer!")
  26. def increment_odometer(self, miles):
  27. """Add the given amount to the odometer reading."""
  28. self.odometer_reading += miles