Skip to content

Instantly share code, notes, and snippets.

@kipkitur
Created July 16, 2020 02:32
Show Gist options
  • Save kipkitur/b71c9585b8e5573550224f4f3389fa6e to your computer and use it in GitHub Desktop.
Save kipkitur/b71c9585b8e5573550224f4f3389fa6e to your computer and use it in GitHub Desktop.
class Car:
#use the init method to create objects/attributes that are members of a class
def __init__(self, make, model, year, horsepower):
# to use the __init__ method you have to \
# initialize attributes to be used for all class methods in this class \
# and for objects created by this class \
# use self argument to make sure that the attribute are available to other class methods so that we can reuse our method with every object that we create for that class
self.make = make
self.model = model
self.year = year
self.horsepower = horsepower
#method 1
def car_description(self):
#short description of the vehicle
print("This is a {} {}. It was manufactured in {} and has a total of {} hp".format(self.make, self.model, self.year, self.horsepower))
#method 2
def car_ranking(self):
if self.horsepower == 90:
print("{} has the lowest ranking. It may not be not be a suitable choice".format(self.horsepower))
elif self.horsepower == 200:
print("{} may not be your only choice if you are tight on a budget".format(self.horsepower))
else :
print("{} is the best in this category. It may be worth your money".format(self.horsepower))
#method 3
def car_manufactured_year(self):
if 2016 <= self.year <= 2020:
print("{} made after {} have modern high tech gadgets installed. All the cars components are electronically monitored".format(self.make, self.year))
else:
print("If you do not drive a luxury car that was manufactured before {}, chances are that your vehicle may lack high tech gadgets".format(self.year))
#instantiate the class
Lexus = Car("Lexus", "ES 350", 2018, 339)
Ford = Car("Ford", "Fiesta", 1994, 90)
Lexus.car_description()
Lexus.car_manufactured_year()
Lexus.car_ranking()
Ford.car_description()
Ford.car_manufactured_year()
Ford.car_ranking()
#inheritance
class CarFeatures(Car):
def __init__(self, interior, exterior, comfort, make, model, year, horsepower):
#use the super method to initialize attributes as they are defined in the parent class
super().__init__(make, model, year, horsepower)
# use self argument to make sure that the attribute are available to other class methods so that we can reuse our method with every object that we create for that class
self.interior = interior
self.exterior = exterior
self.comfort = comfort
#create methods for the CarFeatures class that you just created
#the self argument allows access to the CarFeatures class
#the other argument allows access to the Car class
def seats(self, other):
if self.interior == "leather":
other.make == "Lexus"
elif self.interior == "half tone":
other.make == "Subaru"
else:
other.make == "Ford"
print("Your {} {} has {} seats".format(other.make, other.model, self.interior))
#create car feature to interact with some of the car models you have created
Luxury = CarFeatures("leather", "carbon fiber", "air suspension", "Lexus", "ES 350", 2018, 339)
Luxury.seats(Lexus)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment