繁体   English   中英

我如何从1个循环中获得多个输出?

[英]How do i get multiple outputs from 1 loop?

我试图让我知道每次计算hori_dist时增量的增加。 相反,它只是向我显示最终结果加在一起。 由于我的范围是5,如何获取5个值?

用Google搜索视频,在线搜索,尝试了多种方法

Vx0 = 18
Vy0 = 18
V = 18
Theta = 45
y0 = 1.8    
x0 = 0
p = 1.2#given density
C = 0.3#Drag coefficient
dt = 0.2#Time increment

def x_direction (x0, Vx0, dt):
"""Calculate the distance moved in x axis"""
new_dist = 0
for hori_dist in range(5):
   hori_dist = x0 + Vx0*dt
   new_dist = new_dist + hori_dist
return new_dist

new_dist = x_direction(x0, Vx0, dt)
print ("Distanced moved in x direction is :", new_dist)

要定义范围 ,许多语言都使用花括号{} 但是,Python使用缩进。

因此,如果要打印5次 ,可以将其包含在for循环中。 可能会对您有帮助。

Vx0 = 18
Vy0 = 18
V = 18
Theta = 45
y0 = 1.8    
x0 = 0
p = 1.2 #given density
C = 0.3 #Drag coefficient
dt = 0.2 #Time increment

def x_direction (x0, Vx0, dt):
    """Calculate the distance moved in x axis"""
    new_dist = 0
    for hori_dist in range(5):
        hori_dist = x0 + Vx0*dt
        new_dist = new_dist + hori_dist
        print ("[LOOP] Distance being moved is", hori_dist) #The extra print
        print ("[LOOP] New distance is", new_dist) #Another extra print
    return new_dist

new_dist = x_direction(x0, Vx0, dt)
print ("Distanced moved in x direction is :", new_dist)

像这样在循环中放入一个print语句

hori_dist = x0 + Vx0*dt print (hori_dist)

同样,您不能像在这里x_direction(x0, Vx0, dt)那样调用函数。 您应该传递值而不是变量x_direction(0, 18, 0.2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM