春遊帶了 money 元,零食清單的價格按順序放在 prices 裡。從第一樣開始買,遇到買不起的就停下,後面的都不買了。寫 items_bought(prices, money),返回買了幾樣。
items_bought([3, 5, 2], 10)→3items_bought([4, 8, 1], 10)→1items_bought([], 5)→0def items_bought(prices, money):
count = 0
for price in prices:
# 在這裡寫:錢不夠就 break;
# 夠的話付錢(money 減掉 price),count 加 1
pass
return count
寫好了先點「執行」試試示例,沒問題再點「提交」。