Kodewick
開始學 →
基礎←→0 / 80

水杯還剩多少

#058基礎self.ml -= sip約 4 分鐘

水杯裡有 ml 毫升水,每喝一口少掉 sip 毫升。寫完 Bottle 的 drink(sip):把杯裡的水減掉這一口;不夠喝的時候,水變成 0,不會是負數。

示例
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):
        # 在這裡寫:減掉這一口;小於 0 就改成 0
        pass


def water_left(ml, sips):
    bottle = Bottle(ml)
    for sip in sips:
        bottle.drink(sip)
    return bottle.ml
Ctrl + Enter 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。