[英]How do i get multiple outputs from 1 loop?
I'm trying to get this to show me the incremental increases for each time hori_dist is being calculated. 我试图让我知道每次计算hori_dist时增量的增加。 Instead it is just showing me the final result all added up together.
相反,它只是向我显示最终结果加在一起。 How do i get it to output 5 values since my range is 5?
由于我的范围是5,如何获取5个值?
Googled for videos, searched online, tried this multiple ways 用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)
To define a scope , many languages use the curly braces { } . 要定义范围 ,许多语言都使用花括号{} 。 Python, however, uses indentation.
但是,Python使用缩进。
So, if you want something to be printed 5 times , you include it inside the for loop. 因此,如果要打印5次 ,可以将其包含在for循环中。 May be this will help you.
可能会对您有帮助。
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)
Just put a print
statement in your loop ,like this 像这样在循环中放入一个
print
语句
hori_dist = x0 + Vx0*dt print (hori_dist)
Also, you can't call function like you did here x_direction(x0, Vx0, dt)
. 同样,您不能像在这里
x_direction(x0, Vx0, dt)
那样调用函数。 You should pass values instead of variables x_direction(0, 18, 0.2)
您应该传递值而不是变量
x_direction(0, 18, 0.2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.