Skip to content

Commit 1f216ed

Browse files
authored
Chapter End
1 parent 7580163 commit 1f216ed

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

9. Classes/car.py

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

9. Classes/dog.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Dog():
2+
"""A simple attempt to model a dog."""
3+
4+
def __init__(self, name, age):
5+
"""Initialize name and age attributes."""
6+
self.name = name
7+
self.age = age
8+
9+
def sit(self):
10+
"""Simulate a dog sitting in response to a command."""
11+
print(self.name.title() + " is now sitting.")
12+
13+
def roll_over(self):
14+
"""Simulate rolling over in response to a command."""
15+
print(self.name.title() + " rolled over!")
16+
17+
18+
my_dog = Dog('willie', 6)
19+
your_dog = Dog('lucy', 3)
20+
21+
print("My dog's name is " + my_dog.name.title() + ".")
22+
print("My dog is " + str(my_dog.age) + " years old.")
23+
my_dog.sit()
24+
25+
print("\nMy dog's name is " + your_dog.name.title() + ".")
26+
print("My dog is " + str(your_dog.age) + " years old.")
27+
your_dog.sit()

9. Classes/electric_car.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""A set of classes that can be used to represent electric cars."""
2+
3+
from car import Car
4+
5+
class Battery():
6+
"""A simple attempt to model a battery for an electric car."""
7+
8+
def __init__(self, battery_size=60):
9+
"""Initialize the batteery's attributes."""
10+
self.battery_size = battery_size
11+
12+
def describe_battery(self):
13+
"""Print a statement describing the battery size."""
14+
print("This car has a " + str(self.battery_size) + "-kWh battery.")
15+
16+
def get_range(self):
17+
"""Print a statement about the range this battery provides."""
18+
if self.battery_size == 60:
19+
range = 140
20+
elif self.battery_size == 85:
21+
range = 185
22+
23+
message = "This car can go approximately " + str(range)
24+
message += " miles on a full charge."
25+
print(message)
26+
27+
28+
class ElectricCar(Car):
29+
"""Models aspects of a car, specific to electric vehicles."""
30+
31+
def __init__(self, manufacturer, model, year):
32+
"""
33+
Initialize attributes of the parent class.
34+
Then initialize attributes specific to an electric car.
35+
"""
36+
super().__init__(manufacturer, model, year)
37+
self.battery = Battery()

9. Classes/favorite_languages.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from collections import OrderedDict
2+
3+
favorite_languages = OrderedDict()
4+
5+
favorite_languages['jen'] = 'python'
6+
favorite_languages['sarah'] = 'c'
7+
favorite_languages['edward'] = 'ruby'
8+
favorite_languages['phil'] = 'python'
9+
10+
for name, language in favorite_languages.items():
11+
print(name.title() + "'s favorite language is " +
12+
language.title() + ".")

9. Classes/my_car.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from car import Car
2+
3+
my_new_car = Car('audi', 'a4', 2015)
4+
print(my_new_car.get_descriptive_name())
5+
6+
my_new_car.odometer_reading = 23
7+
my_new_car.read_odometer()

9. Classes/my_cars.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from car import Car
2+
from electric_car import ElectricCar
3+
4+
my_beetle = Car('volkswagen', 'beetle', 2015)
5+
print(my_beetle.get_descriptive_name())
6+
7+
my_tesla = ElectricCar('tesla', 'roadster', 2015)
8+
print(my_tesla.get_descriptive_name())

0 commit comments

Comments
 (0)