0

i'm very new in python and i don't understand what the error say and what i do wrong

any help would be appreciated

thanks

my code:

class TSP:
    def __init__(self, initial_node, adjacency_matrix):
        self.initial_node = initial_node
        self.adjacency_matrix = adjacency_matrix

    stack = {"cost": {}, "distances": {}}

    def distance(self, start_node, end_node):
        self.stack["distances"]["dist%s-%s" %(start_node, end_node)] = self.adjacency-matrix[start_node][end_node]

    def cost(self, visit_nodes, end_node_cost):
        if len(visit_nodes) == 2:
            node_set = visit_nodes.remove(end_node_cost)
            self.distance(node_set[0], end_node_cost)
        print (self.stack)


test = TSP(1, [[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]])

print (test.cost([1, 2], 2))

and my error is :

TypeError: 'NoneType' object has no attribute '__getitem__'

1 Answer 1

1

You need to study the backtrace to discover where the error is coming from. visit_nodes.remove() returns None (see the official docs for the list.remove method—no return value is mentioned). Therefore node_set is None. Therefore node_set[0] in the subsequent line fails. You presumably mean to dereference the list itself, i.e. visit_nodes[0].

Sign up to request clarification or add additional context in comments.

2 Comments

This won't be the last of your problems however. (1) You'll presumably be able to fix the KeyError resulting from using "distance" where you mean "distances". (2) You'll also have to program around the fact that the "distances" sub-dictionary is empty when you appear to want to use it. (3) You may not notice, until much much later in your development, the insidious effect of calling .remove() on a list that gets passed into the cost() method, which has the (possibly undesirable) effect of modifying the original object that was passed.
ok i corrected your sugestions and i still get that typeError

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.