如何为该问题编写python代码?

时间:2019-05-29 16:28:13

标签: python-3.x

蜗牛落在125厘米井的底部。蜗牛每天上升30厘米。但是到了晚上,由于墙壁潮湿,所以在睡觉时会滑动20厘米。从井中逃脱需要多少天?

1 个答案:

答案 0 :(得分:0)

well = 125
cmPerDay = 30
cmPerNight = -20
snailHasNotEscaped = True
days = 0
totalCMs = 0

cmPerWholeDay = cmPerDay + cmPerNight

while snailHasNotEscaped:
   totalCMs += cmPerWholeDay
   days += 1
   print(days)
   if totalCMs >= well:
      snailHasNotEscaped = False

print("Congratulations! the snail escaped in", days, "days")