Kodewick
Start learning →
Basic←→0 / 100

Snack money

#024BasicLoopsLists4 min

You take money on the school trip, and your snack list's prices are in prices, in order. Buy from the first one, and stop at the first you cannot afford, skipping the rest. Write items_bought(prices, money) to return how many you bought.

Examples
items_bought([3, 5, 2], 10)→3
items_bought([4, 8, 1], 10)→1
items_bought([], 5)→0
main.py
def items_bought(prices, money):
    count = 0
    for price in prices:
        # your code here: not enough money, break;
        # otherwise pay (take price off money), add 1 to count
        pass
    return count
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.