我正在用Python做关于航班价格的课程。我的布尔函数返回是现在购买机票还是等待更长时间购买机票,其中True表示现在购买,False表示等待更长时间:
def should_I_buy(data, input_price, input_day):
"""Returns whether one should buy flight ticket now or wait longer to buy"""
for day, price in data:
if day < input_day:
if price < input_price:
return False
return True
我还想找到一种方法,当我输入一个随机的输入价格和输入日期时,计算循环中有多少对错。你知道吗
可以使用
sum
对for循环中的所有True进行计数,(True=1
,False=0
):测试和输出:
希望这对您有所帮助,如果您还有其他问题,请发表评论。:)
那么,您应该使用一个在每次迭代中递增的变量:
NB
请注意,我不知道你在做什么,所以我的答案是基于快速查看你的代码。你知道吗
如果这不符合预期,请评论,我们可以通过您希望获得什么。你知道吗
相关问题 更多 >
编程相关推荐