水果攤用字典記庫存:水果對個數。寫 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}def sell(stock, fruit, n):
# 在這裡寫:fruit 的個數減去 n
return stock
寫好了先點「執行」試試示例,沒問題再點「提交」。