我有一个while循环,它返回爬起山坡所需的'rush'数量。 山的大小是'坡高',高度上升是'rush_height_gain'减去'back_sliding'。while循环(Python)
下面的代码适用于:
ans = num_rushes(15, 10, 5)
print(ans)
它打印1
和
ans = num_rushes(100, 15,7)
print(ans)
它打印2
和
ans = num_rushes(10, 10, 9)
print(ans)
它打印12
但返回
ans = num_rushes(100, 10, 0)
print(ans)
错误的答案应该打印10,而是打印9
我不知道这是为什么,任何帮助将是不胜感激
def num_rushes(slope_height, rush_height_gain, back_sliding):
current_height = 0
rushes = 0
while current_height < slope_height:
if rush_height_gain == slope_height:
rushes+=1
return rushes
elif current_height < slope_height:
if current_height == slope_height:
return rushes
else:
a = rush_height_gain - back_sliding
current_height += a
if current_height == slope_height:
return rushes
elif current_height > slope_height:
return rushes
else:
rushes+=1
return (rushes)
应该怎么办?大多数人不想猜测。 – squiguy
您可以添加实际和预期的输出吗? – jpmc26
http://pythonfiddle.com/rushes-quiz-work/ – perreal