一個函數使用xyz,然後在另一個函數中調用這些變量。函數距離應該從readast中獲取每個xyz,然後每個行使用square,sum和square root。例如500 600 700(每個數字平方,將它們全部加起來,然後平方和)它將取得該結果並在行結束後顯示它。然而,我的距離數字只是我的readas函數中的最後一行數字......任何想法爲什麼?在for循環中創建相同數字的隨機數生成器Python
def readast():
astlist=open('asteroids.txt','w')
letter=65
for line in range(15):
x=random.randint(1,1000)
y=random.randint(1,1000)
z=random.randint(1,1000)
astlist.write(('\n')+chr(letter)+('\t')+(str(x))+('\t')+(str(y))+('\t')+(str(z)))
letter=letter+1
return astlist,x,y,z
astlist.close()
def distance(astlist,x,y,z):
distlist=open('distance.txt','w')
letter=65
for line in range(15):
x1=x**2
y1=y**2
z1=z**2
equation=math.sqrt(x1+y1+z1)
distlist.write(('\n')+chr(letter)+('\t')+(str(x))+('\t')+(str(y))+('\t')+(str(z)+('\t')+(str(equation))))
letter=letter+1
return distlist
distlist.close()
一個函數被稱爲「讀」,但這兩個函數都不讀任何東西。函數中的'return'之後的代碼將不會運行。你明確地在'readast()'的'return'行中返回一組'x,y,z'值。 – 2014-11-23 04:49:16