简体   繁体   中英

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. 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?

Googled for videos, searched online, tried this multiple ways

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.

So, if you want something to be printed 5 times , you include it inside the for loop. 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

hori_dist = x0 + Vx0*dt print (hori_dist)

Also, you can't call function like you did here x_direction(x0, Vx0, dt) . You should pass values instead of variables x_direction(0, 18, 0.2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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