Kodewick
开始学 →
基础←→0 / 100

预算内最好的礼物

#093基础列表条件约 4 分钟

给朋友挑生日礼物,手上有 budget 元。写 best_gift(prices, budget),在价钱不超过 budget 的礼物里,返回最贵的那个价钱。一个都买不起(或列表是空的)就返回 0。

示例
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:
        # 在这里写:买得起,又比 best 贵,就记下它
        pass
    return best
Ctrl + Enter 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。