擅长:python、mysql、java
<p>可以使用<code>sum</code>对for循环中的所有True进行计数,(<code>True=1</code>,<code>False=0</code>):</p>
<pre><code>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)
</code></pre>
<p>测试和输出:</p>
<pre><code>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
</code></pre>
<p>希望这对您有所帮助,如果您还有其他问题,请发表评论。:)</p>