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

露營裝備合起來

#037基礎total.get(thing, 0) + n約 4 分鐘

你和朋友要一起去露營,各列了一份裝備 {東西: 幾個}。寫 pack_together(mine, yours),返回合起來的清單:兩邊都有的數量相加,先是你的東西,再是朋友多出來的。

示例
pack_together({"tent": 1, "torch": 2}, {"torch": 1, "stove": 1})→{'tent': 1, 'torch': 3, 'stove': 1}
pack_together({}, {})→{}
main.py
def pack_together(mine, yours):
    total = dict(mine)
    for thing, n in yours.items():
        # 在這裡寫:把朋友的這樣東西加進 total(total 裡沒有就從 0 開始)
        pass
    return total
Ctrl + Enter 也能執行
結果

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