Kodewick
Start learning →
Basic←→0 / 100

Best gift on a budget

#093BasicListsConditions4 min

You are choosing a birthday present with budget to spend. Write best_gift(prices, budget) to return the highest price that is not over budget. If none fit (or the list is empty), return 0.

Examples
best_gift([35, 80, 50, 120], 60)→50
best_gift([99, 150], 50)→0
main.py
def best_gift(prices, budget):
    best = 0
    for p in prices:
        # your code here: if you can afford it and it beats best, keep it
        pass
    return best
Ctrl + Enter runs too
Result

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