Kodewick
Start learning →
Basic←→0 / 80

Water bottle

#058BasicClasses4 min

A bottle holds ml millilitres of water, and each sip takes sip millilitres. Finish drink(sip) in Bottle: take the sip out of the bottle; if there isn't enough, the bottle ends at 0, never below.

Examples
water_left(500, [100, 150])→250
water_left(300, [200, 200])→0
main.py
class Bottle:
    def __init__(self, ml):
        self.ml = ml

    def drink(self, sip):
        # your code here: take away the sip; if it goes below 0, set it to 0
        pass


def water_left(ml, sips):
    bottle = Bottle(ml)
    for sip in sips:
        bottle.drink(sip)
    return bottle.ml
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.