冰箱上的便籤寫著 "milk x2, bread, eggs x12":每樣用逗號隔開, x 後面是要幾個,沒寫就是 1 個。寫 count_items(note),返回一共要買幾個東西。空便籤返回 0。
count_items("milk x2, bread, eggs x12")→15count_items("")→0def count_items(note):
if note.strip() == "":
return 0
total = 0
for part in note.split(","):
part = part.strip()
# 在這裡寫:用 find 找 " x",找到就加上後面的數,找不到就加 1
pass
return total
寫好了先點「執行」試試示例,沒問題再點「提交」。