0

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()
4
  • You needn't do your own distance() computations, just use the math.hypot() function. Commented Nov 22, 2014 at 21:35
  • When is the distance function used? Is it called from readast? Commented Nov 22, 2014 at 21:37
  • @Adamantite, the 'distance' function is called in the main function, which isn't shown here. My professor says each function should do only one thing, or else I would put the appending into the same function as 'readast'. The distance function is used to create a new text file, distance.txt with the same values as asteroids.txt, then append more values into distance.txt if that makes any sense Commented Nov 22, 2014 at 21:40
  • You've opened the 'asteroids.txt' file for reading in your distance() function. Just read each x, y, and z from it, compute the distance using those values, and then write the results to the other file. Commented Nov 22, 2014 at 21:42

3 Answers 3

1

You're returning (x,y,z) in the first function, which is called by the main function? Make sure that your main function assigns the tuple to something, then pass it as parameters into the second function...

Simplified:

def distance(x,y,z):

   ....



def main():

   ...
   (x ,y ,z) = readast()
   ...

   distance(x,y,z)
Sign up to request clarification or add additional context in comments.

Comments

1

If you can modify the function signatures, parameterize distance:

def distance(x, y, z):

then when you call readast from main, grab the return values:

x, y, z = readast()

and pass x, y, and z as arguments when you call distance from main:

distance(x, y, z)

Note that there are several local variables named x. You are not sharing a local variable between several functions; only its value. Function calls copy the values of arguments into parameters, and then evaluate to their returned value(s).

Comments

0

The simplest way I think it's through function parameters

def distance(_x, _y, _z):
  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)
  ...

but I think you need to think again the solution, you could make a function like this:

def equation(x, y,z):
   return math.sqrt(math.pow(x,2)+math.pow(y,2)+math.pow(z,2))

and then call it when you right the first file

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)+ '\t' +str(equation(x,y,z)))
    letter= letter+ 1
outFileA.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.