I have created a text file in one function. For a school project, I have to take that text file, and use the same data to put into another text file, "distance" and then append the variable "equation" to the end of each row in the previous text file. However, I'm stuck on how I can take the x,y,z variables in the first function, and use them in the second function without using a global variable? Help!
def readast():
astlist=[]
outFileA=open('asteroids.txt','w')
letter=65
size_of_array=15
astlist=[]*size_of_array
for i in range(0,size_of_array):
x=random.randint(1,1000)
y=random.randint(1,1000)
z=random.randint(1,1000)
outFileA.write ('\n'+chr(letter) + '\t' +(str(x)) + '\t' + (str(y)) +'\t' +(str(z)))
letter= letter+ 1
return x,y,z
outFileA.close()
def distance():
outFileA=open('asteroids.txt','r')
outFileD=open('distance.txt','w')
x= (x**2)
y= (y**2) #these three variables I need to pull from readast
z= (z**2)
equation=math.sqrt(x+y+z)
for row in range(len(outfileA)):
x,y,z=outFileA[row]
outFileD.append(equation)
outFileD.close()
distance()computations, just use themath.hypot()function.distancefunction used? Is it called fromreadast?'asteroids.txt'file for reading in yourdistance()function. Just read eachx,y, andzfrom it, compute the distance using those values, and then write the results to the other file.