Вы можете использовать sum
для подсчета всех значений True в for, (True=1
, False=0
):
def should_I_buy(data, input_price, input_day):
"""Returns whether one should buy flight ticket now or wait longer to buy"""
return sum(day >= input_day or price >= input_price for day, price in data)
тест и вывод:
data = [(14, 77.51), (13, 14.99), (12, 56.09), (11, 14.99), (10, 14.99), (9, 14.99), (8, 39.00), (7, 114.23),
(6, 37.73), (5, 56.09), (4, 14.99), (3, 22.43), (2, 22.43), (1, 31.61), (0, 168.29)]
print(should_I_buy(data, 50.00, 8)) # output 10
print(should_I_buy(data, 18.00, 3)) # output 15
Надеждаэто поможет вам, и прокомментируйте, если у вас есть дополнительные вопросы.:)