예제 #1
0
def test_electric_car_charge():
    ec = ElectricCar(100)
    assert ec.drive(70) == 70
    assert ec.drive(50) == 30
    assert ec.drive(50) == 0
    ec.charge()
    assert ec.drive(50) == 50
예제 #2
0
class ElectricCar(Car):

    def __init__(self, make, model, year):
        super().__init__(make, model, year)
        self.battery_size = 70
        self.charge_level = 0

    def charge(self):
        self.charge_level = 100
        print("The vehicle is fully charged")

    def fill_tank(self):
        print("This car has no fuel tank!")

my_ecar = ElectricCar('tesla', 'model s', 2016)
my_ecar.charge()
my_ecar.drive()


# Instances as attributes

class Battery():
    def __init__(self, size=70):
        self.size = size
        self.charge_level = 0
    def get_range(self):
        if self.size == 70:
            return 240
        elif self.size == 85:
            return 270
예제 #3
0
my_beetle.drive()

##################################################################################

# Storing objects in a list
# List can hold large number of objects from a class.
from car import Car, ElectricCar

# Make lists to hold a fleet of cars.
gas_fleet = []
electric_fleet = []

# Make 500 gas_cars and 250 electric_cars.
for a in range(500):
    car = Car('ford', 'focus', 2016)
    gas_fleet.append(car)
for b in range(250):
    ecar = ElectricCar('nissan', 'Leaf', 2016)
    electric_fleet.append(ecar)

# Fill the gas_cars & Charge the electric cars
for car in gas_fleet:
    car.fill_tank()
for ecar in electric_fleet:
    ecar.charge()

print("gas_cars:", len(gas_fleet))
print("electric_cars:", len(electric_fleet))

# It is seen that this trick is used many time in Software Devlopement programing as well as in Data-Science projects
from car import Car, ElectricCar

my_beetle = Car('volkswagen', 'beetle', 2016)
my_beetle.fill_tank()
my_beetle.drive()

my_tesla = ElectricCar('tesla', 'model s', 2016)
my_tesla.charge()
my_tesla.drive()