Traveling salesperson project

Welcome to the Get Help category!

This is where you can ask questions about your code. Some important things to remember when posting in this category :slight_smile:

  • Learn how to ask a good question and get a good answer!
  • Remember to include a link to the exercise you need help with!
  • If someone answers your question, please mark their response as a solution :white_check_mark:
  • Once you understand a new concept, come back and try to help someone else! }

so im all done with the project but for some reason I get this error:

Traceback (most recent call last):
File “script.py”, line 78, in
traveling_salesperson(graph)
File “script.py”, line 52, in traveling_salesperson
while not visited_all_vertices:
UnboundLocalError: local variable ‘visited_all_vertices’ referenced before assignment

“UnboundLocalError: local variable ‘visited_all_vertices’ referenced before assignment”
Where is ‘visited_all_vertices defined?

More here:
https://docs.python.org/3.1/faq/programming.html?highlight=nonlocal#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value

1 Like

Thanks, just one question though. I got no errors but is there supposed to be no code? what’s supposed to appear? I got blank, is that fine?

I moved the two last lines to right once for it to not produce error

Is there a link to the lesson? What does it say under “Get Unstuck”?

No unstuck there, no solution

Project link: https://www.codecademy.com/projects/practice/traveling-salesperson

1 Like

Instruction #16 says you should print the final path.
image

1 Like

how do I do that though?

Before I try to help you with the specific issue, tell me why you moved the last 2 lines of your code to the right. What was your thinking behind the decision? What effect did it have? Just to make sure we are all on the same page, the last 2 lines of your code are:

  graph = build_tsp_graph(False)
  traveling_salesperson(graph)
1 Like

I guess it helped with the debugging, so what am I missing? or am ?

Do you mean how do you “print the final path computed by the algorithm”?

What was the effect?

Answer

You moved your only function calls inside of the traveling_salesperson function, so none of your functions are executed when you click Save. Your code really does nothing now.

I get the feeling that you may be in over your head with these projects. Are you writing the code you are sharing yourself?

1 Like

Yes I did write em, so how do I fix that though I don’t know

yes I mean that--------------

So how do I get the code to print out? what’s wrong?

Well, from the last update to your code, you moved the function calls from where they should be to inside one of the functions to be called. You need to undo that first, and then deal with the original error that you had when you started this topic.

Line 52 of your code is shown in the error message. The error message tells you specifically what the problem is. It’s telling you that visited_all_vertices has not been assigned yet, but you are trying to query its assigned value. Where in your code is there an assignment to visited_all_vertices?

Here is a very simple example of code that throws the same error
def do_stuff():
    print(message)
    message = "Hello, World!"

do_stuff()

Output:

Traceback (most recent call last):
File “”, line 1, in
File “”, line 2, in do_stuff
UnboundLocalError: local variable ‘message’ referenced before assignment

How would you fix this?

1 Like

Ok so I finished the project correctly now, turn out there were some bugs I hadn’t fixed. Thanks for motivating me forward

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.