Learn Algorithm Design by Building a Shortest Path Algorithm - Step 21

Tell us what’s happening:

Describe your issue in detail here.
I have tried replacing the my_graph in the for statement with unvisited and shortest_path but neither works. At this stage it’s telling me to apppend each node, but is that not what I did?

Your code so far

my_graph = {
    'A': [('B', 3), ('D', 1)],
    'B': [('A', 3), ('C', 4)],
    'C': [('B', 4), ('D', 7)],
    'D': [('A', 1), ('C', 7)]
}


/* User Editable Region */

def shortest_path(graph, start):
    unvisited = []
    for my_graph in graph:
        unvisited.append('A', 'B', 'C', 'D')

/* User Editable Region */

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 OPR/105.0.0.0 (Edition std-1)

Challenge Information:

Learn Algorithm Design by Building a Shortest Path Algorithm - Step 21

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

Create a for loop to iterate over your graph, and append each node to the unvisited list.

You have a for loop iterating over the graph :white_check_mark:

for my_graph in graph:

A For loop defines a new variable here. You can’t use my_graph because that is already defined above, it’s taken.

append each node to the unvisited list.

Since your loop is looking at “each node” in the graph, you could call your new variable node.

for `<new variable>` in graph:
unvisited.append('A', 'B', 'C', 'D')

within the loop, each node is stored in the new variable, 1 at a time, for each loop. Imagine you didn’t know the contents of the graph, but you want to append the node to the list: You would append the variable which stores the node.

Take a minute to review how a for loop operates, you will use for loops a lot in Python it’s worth understanding deeply:
https://www.w3schools.com/python/python_for_loops.asp

https://www.geeksforgeeks.org/python-for-loops/

1 Like

I appreciate this break down, and explanation for the for loop. I made the correction and used node as the new variable. I kept the append the same but I am still getting the error that they are not appended

What is stored in the node variable?

The main problem is that you don’t understand how the for loop works. You should really take the time to understand it, and review the links I sent.

After that if you have any questions about it, just let me know :+1:

1 Like

that helped, thank you. I kept thinking I needed to put the values and not simply the variable. Going to keep those sites bookmarked so I can remind myself of that

Both of those sites are great for learning a bit more about any concept, they usually come up if you search for “python ”

Anytime you have a for loop, you know you are going to be using that variable:

for item in list:
    print(item)

I hope you have a better idea of it now, you’ll be using it a lot to loop through lists.

1 Like

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