0

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)
2
  • 1
    You need to show us the code that calls these functions. The ideal question is a SSCCE—something we can run, against your input, and see how the output is different from the expected output. Commented Mar 18, 2013 at 17:52
  • Meanwhile, it's pretty confusing to have global variables, function parameters, and local variables all with the same name, and it's a red flag that you're expecting some kind of magic in the code we aren't seeing. For example, the node that gets set in start_node isn't the global. It is getting returned, but unless the caller is doing something like node = start_node(Alist) to set the global, the global isn't going to change, so the value will still be 0. Commented Mar 18, 2013 at 17:53

2 Answers 2

2

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()
Sign up to request clarification or add additional context in comments.

Comments

-1

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.

1 Comment

This isn't his problem. He has an Alist = create_matrix(file), which replaces the global with the value returned by this function. Also, as he says in the question, "Alist is printing fine". It's node that isn't working. And meanwhile, encouraging him to switch from passing and returning variables to sharing globals is not a very good design choice.

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.