3 Replies - 1052 Views - Last Post: 15 March 2011 - 08:11 AM Rate Topic: -----

#1 kite   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 08

Passing Arguments Between Functions

Posted 10 March 2011 - 04:30 PM

Hello,

I am having some trouble in passing arguments between functions. This is my code:

infinity = 1000000
invalid_node = -1
startNode = 0

#Values to assign to each node
class Node:
     distFromSource = infinity
     previous = invalid_node
     visited = False

#read in all network nodes
def network():
    f = open ('network.txt', 'r')
    theNetwork = [[int(node) for node in line.split(',')] for line in f.readlines()]
    print theNetwork

    return theNetwork

#for each node assign default values    
def populateNodeTable(): 
    nodeTable = []
    index = 0
    f = open('network.txt', 'r')
    for line in f: 
      node = map(int, line.split(',')) 
      nodeTable.append(Node())
      
      print "The previous node is " ,nodeTable[index].previous 
      print "The distance from source is " ,nodeTable[index].distFromSource 
      index +=1
    nodeTable[startNode].distFromSource = 0 

    return nodeTable

#find the nearest neighbour to a particular node
def nearestNeighbour(currentNode, theNetwork):
     nearestNeighbour = []
     nodeIndex = 0
     for node in nodeTable:
          if node != 0 and currentNode.visited == false:
             nearestNeighbour.append(nodeIndex)
             nodeIndex +=1

     return nearestNeighbour

currentNode = startNode

if __name__ == "__main__":
    nodeTable = populateNodeTable()
    theNetwork = network()
    nearestNeighbour(currentNode, theNetwork)



This is the error message that I get:

if node != 0 and currentNode.visited == false:
AttributeError: 'int' object has no attribute 'visited'

How can I 'connect' the values in my class Node, populateNodeTable and nearestNeighbour functions so that I can populate my nearestNeighbour list?

Is This A Good Question/Topic? 0
  • +

Replies To: Passing Arguments Between Functions

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: Passing Arguments Between Functions

Posted 15 March 2011 - 07:42 AM

could you please provide the network.txt file?

EDIT:
Sorry it's taken me so long to get to this one. Getting close to the end of a sprint at work so things are craaaaazy right now.

This post has been edited by atraub: 15 March 2011 - 07:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Passing Arguments Between Functions

Posted 15 March 2011 - 08:10 AM

startNode = 0

# ...
# why is this defined in the middle of nowhere?
currentNode = startNode

if __name__ == "__main__":
# ...
# yep, currentNode is at best 0, at worst None
	nearestNeighbour(currentNode, theNetwork)



You need to decide if nodes are just int or if they are an instance of the Node class. Pick one.
Was This Post Helpful? 1
  • +
  • -

#4 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: Passing Arguments Between Functions

Posted 15 March 2011 - 08:11 AM

How did I overlook that? Good catch.

This post has been edited by atraub: 15 March 2011 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1