【问题标题】:'While' loop count is worng?“虽然”循环计数错误?
【发布时间】:2013-08-22 08:56:15
【问题描述】:
ans = num_rushes(100, 15, 7)
print(ans) 

应该返回 19,但我的返回 12?

这是我所拥有的:

def num_rushes(slope_height, rush_height_gain, back_sliding):
    rushes = 0
    current_height = 0
    rush_height_counter = 0
    back_sliding_counter = 0
    rush_height_gain = 0.95 ** rush_height_counter * rush_height_gain
    back_sliding = 0.95 ** back_sliding_counter * back_sliding

    while current_height < slope_height:
        current_height = current_height + rush_height_gain
        rushes += 1
        rush_height_counter += 1
        back_sliding_counter += 1

        if current_height < slope_height:
            current_height = current_height - back_sliding

        else:
            return rushes

【问题讨论】:

    标签: python-3.x while-loop iteration


    【解决方案1】:

    这段代码0.95 ** rush_height_counter * rush_height_gain是什么意思

    0.95 ** (rush_height_counter * rush_height_gain)
    

    (0.95 ** rush_height_counter) * rush_height_gain
    

    我认为你想要第一个,但 python(据我所知)做第二个!

    【讨论】:

      猜你喜欢
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2016-06-14
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多