0

I'm having trouble with python. I keep getting the same error:

Traceback (most recent call last):  
  File "F:\test4", line 21, in <module>  
    graph = dict([(label, node(label))] for label in node_labels)  
  File "F:\test4", line 21, in <genexpr>  
    graph = dict([(label, node(label))] for label in node_labels)  
NameError: global name 'node' is not defined  
# open network.txt and populate nodes and close file
network_file = open("network.txt", "r")
lines = [line.strip() for line in network_file]
network_file.close()
print (len(lines))


# edges which will be populated with information in network.txt
edges = []                      # list of <label, label, distance> triples
node_labels = set()             # set of node labels
graph = {}                      # dictionary of nodes keyed by labels

for line in lines:
    strNode, strNeighbor, strMetric = line.split()[:3]
    intMetric = int(strMetric)

    edges.append((strNode, strNeighbor, intMetric))
    node_labels.update([strNode, strNeighbor])

# create graph
graph = dict([(label, node(label))] for label in node_labels)

up to this line, I can't find any problem with the global variable node, it should work.

Thanks!

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
Ranger
  • 653
  • 3
  • 7
  • 11

2 Answers2

2

In the last line you are calling node(label). Have you defined the function node?

dfan
  • 5,714
  • 1
  • 31
  • 27
2

Why should it? node is nowhere defined in the code you showed... maybe you forgot an import?

Freax
  • 91
  • 2