Kodewick
开始学 →
基础←→0 / 80

水杯还剩多少

#058基础类和对象约 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 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。