简体   繁体   中英

python passing variables between functions

I am trying to pass an integer to a function. I think it might not be working because I am calling it to many times? For example I create a 2d matrix in one function called Alist and then return it. With a second function I pass Alist and specify a location for the value I want from Alist which I then return. Finally (so far), a third function will ask for the returned value and Alist. Alist is printing fine but the returned value (node) is printing 0 when it should be 4. I guess that it is using the node = 0 variable declared at the top of the code but I am not sure why.

The first line of network.txt looks like this: 0,2,4,1,6,0,

Alist = []
node = 0

file = ("F:/media/KINGSTON/Networking/network.txt")

def create_matrix(file):
    with open('network.txt') as f:
        Alist = []
        for line in f:
            part = []
            for x in line.split(','):
                part.append(int(x))
            Alist.append(part)
    return Alist

def start_node(Alist):
        node = Alist[0][2]
        print (node)
        return node

#test neighbours to see if they can be used
def check_neighbours(node, Alist):
        print (Alist)
        print (node)
        #check for neighbours. if we start at [0][0] we must look for [0][1]
        #and [1][0]. Figure out how to add 1 to each cell to navigate across.

#running of code begins here
Alist = create_matrix(file)
start_node(Alist)
check_neighbours(node, Alist)

Here's your problem, on the second line of "running of code begins here":

Alist = create_matrix(file) 
start_node(Alist) 
check_neighbours(node, Alist)

When you call start_node(Alist) , it creates a local variable (which happens to be called node ) and returns its value, which you just ignore. This means the global variable node (despite the coincidental name) is not being changed, so it's still 0.

To make this work, you need to do the same thing you do on the line above:

node = start_node(Alist) 

However, to make your code less confusing, you really should do a few things:

First, remove the Alist = [] and node = 0 at the top. Defining them before the functions makes it look like you expect them to be used as globals within the functions, which is misleading. (And the same goes for file —you do need that defined, but not at the top.)

Then, if you abstract all the top-level stuff (including those two global variables) into a function, this takes away all possibility for confusion.

So, leave your three function definitions, then:

def main():
    file = ("F:/media/KINGSTON/Networking/network.txt")
    Alist = create_matrix(file)
    node = start_node(Alist)
    check_neighbours(node, Alist)
main()

In function create_matrix when you're writing Alist = [] you're creating a new local variable Alist which shadows global variable Alist . Try the following:

def create_matrix(file):
    global Alist  # Mark Alist as global variable
    with open('network.txt') as f:
        Alist = []
        for line in f:
            part = []
            for x in line.split(','):
                part.append(int(x))
            Alist.append(part)
    return Alist

See more in global keyword documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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