選擇會記在這個瀏覽器裡
sum(pets.values())
寵物店用字典記每種寵物有幾隻,比如 {"cat": 3, "dog": 2}。寫 total_pets(pets),返回一共幾隻。字典是空的就返回 0。
{"cat": 3, "dog": 2}
total_pets(pets)
0
total_pets({"cat": 3, "dog": 2})
5
total_pets({"rabbit": 4, "fish": 10, "bird": 1})
15
def total_pets(pets): total = 0 # 在這裡寫:把每種寵物的只數加起來 return total
寫好了先點「執行」試試示例,沒問題再點「提交」。
這一頁要開啟 JavaScript 才能寫程式碼。