簡體   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