python - 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))
解决方案
您的 return 语句缩进一级太多,因此循环在一次后终止。确保它与 while 对齐。
推荐阅读
- powerbi - 如何在 Power BI 中的多对多关系中引用正确的值?
- bash - Scrapy 命令产生 'bash: cd: too many arguments'
- jsf - 如何在jsf中编辑数据表
- regex - Is there an easy way to distinguish Lastnames and Firstnames using RegEx in Notepad++
- plot - 在 SageMath 中的图中的顶点上绘制非内射标签
- c# - 使用 json.net newtonsoft 将 csv 序列化为带有标头记录的 json
- c++ - 重定位图像时如何前进到下一个内存块
- hyperledger-fabric - 将背书策略更改为需要多个成员,但不确定如何让所有对等方背书
- c++ - 在为 LINUX 创建共享库时如何避免 STL 的私有/弱副本
- javascript - 使用“set”使用 Mongoose 更新现有子文档的数组元素有困难吗?