1. Give a Laptop a Battery
Write code Easy ✓ CompletedBattery is given. Write a class Laptop that has a battery:
__init__(self, model, battery)stores both onself(asmodelandbattery).work(self, hours)delegates to the battery by callingself.battery.drain(...)withhours * 15.charge_left(self)returns the battery'slevel.
class Battery:
def __init__(self, level=100):
self.level = level
def drain(self, amount):
self.level = max(0, self.level - amount)
# Write class Laptop below.work, do not change the level yourself. Hand the job to the inner object: self.battery.drain(hours * 15).class Battery:
def __init__(self, level=100):
self.level = level
def drain(self, amount):
self.level = max(0, self.level - amount)
class Laptop:
def __init__(self, model, battery):
self.model = model
self.battery = battery
def work(self, hours):
self.battery.drain(hours * 15)
def charge_left(self):
return self.battery.level
laptop = Laptop("Aero", Battery())
laptop.work(2)
print(laptop.charge_left())