简体   繁体   English

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

[英]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.

相关问题 如何从 kubeflow 并行 for 循环收集所有输出? - How do I collect all outputs from a kubeflow parallel for loop? 如何在 python 中获得 2 个联立方程的多个输出? - How do I get multiple outputs for 2 simultaneous equations in python? 我如何编译并引入来自同一个工作人员的多个输出? - How do I compile and bring in multiple outputs from the same worker? 如何使用For循环从html获取多个链接? - How do I use For Loop to get multiple links from an html? 如何通过变量/定义的术语插入多个输入并使用代码获得多个输出? (Python) - How do I insert multiple inputs via a variable/defined term and get multiple outputs with a code? (Python) 如何在循环中打印调用subprocess.Popen(...)的输出? - How do I print outputs from calls to subprocess.Popen(…) in a loop? 如何让输出在一行中,而不是多个? (Jupyter 笔记本) - How do I get the outputs to be in one row, instead of multiple? (Jupyter Notebook) 如何在 python.netmiko 中从循环中获取多个输出到外部) - how to take multiple outputs from a loop to outside in python(netmiko) 如何在for循环中获得输出总和 - How to get sum of outputs in for loop 如何将python中for循环的多个输出保存到文本文件? - How to save multiple outputs from a for loop in python to a text file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM