Kodewick
開始學 →
入門←→0 / 112

賣掉幾個

#058入門stock[fruit] -= n約 3 分鐘

水果攤用字典記庫存:水果對個數。寫 sell(stock, fruit, n):賣出 n 個 fruit,把它的個數減掉,返回改好的字典。fruit 一定在字典裡。

示例
sell({"apple": 10, "pear": 4}, "apple", 3)→{'apple': 7, 'pear': 4}
sell({"apple": 10, "pear": 4}, "pear", 4)→{'apple': 10, 'pear': 0}
main.py
def sell(stock, fruit, n):
    # 在這裡寫:fruit 的個數減去 n

    return stock
Ctrl + Enter 也能執行
結果

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