首页 > 解决方案 > Python on while 返回值

问题描述

嗨,我在 python 中有这段代码,我想要返回并继续打印,但它只返回一次并打印一次,这是我尝试过的

def car(distance,speed):
  while distance>0:
   distance=distance-20
   speed=speed-speed*0.05
   print('distance=',distance, 'speed is',speed)
   # Here's what i tried return but it just print one time
   return distance,speed

print(car(220,160))

标签: pythonloopsreturn

解决方案


您的 return 语句缩进一级太多,因此循环在一次后终止。确保它与 while 对齐。


推荐阅读