'虽然'循环计数错了?

时间:2013-08-20 10:33:13

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

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

1 个答案:

答案 0 :(得分:2)

使用这段代码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(据我所知)做第二个!