简体   繁体   English

在 Python 中使用另一个函数中的局部变量?

[英]Using local variables from one function in another in Python?

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?但是,我被困在如何在第一个函数中获取 x,y,z 变量,并在不使用全局变量的情况下在第二个函数中使用它们? 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()

If you can modify the function signatures, parameterize distance :如果您可以修改函数签名,请参数化distance

def distance(x, y, z):

then when you call readast from main , grab the return values:然后当您从main调用readast时,获取返回值:

x, y, z = readast()

and pass x , y , and z as arguments when you call distance from main :并在从main调用distance时将xyz作为参数传递:

distance(x, y, z)

Note that there are several local variables named x .请注意,有几个名为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).函数调用将参数的值复制到参数中,然后计算它们的返回值。

You're returning (x,y,z) in the first function, which is called by the main function?您在主函数调用的第一个函数中返回 (x,y,z) ? 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)

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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