Learn-algorithm-design-by-building-a-shortest-path-algorithm step 23

# This is my code and I think it is correct but still doesn't pass: indentation error
def shortest_path(graph, start):
    unvisited = []
    distances = {0}
    for node in graph:
     unvisited.append(node)
     if node == start:

I am not sure when the step 23 asks "Then assign 0 to that node inside the distances dictionary.
In python I can add an item to the dictionary using a index key and assigning a value to it, right?? so inside distances I should have:

# This is my observatioin
distances = {'start': 0} 

What I am doing wrong here??
Thanks

It’s not passing because you need something in the if statement.
Do not modify this line, it’s not mentioned in the instructions:

distances = {} 

For the assignment it’s asking

“The node” it’s referring to is node, not start. You’ve already defined the distances dictionary, and assigned it an empty dictionary, so you don’t want to define it again.

How would you access the node key of distances and assign it 0?

to answer your question I was thinking of the code below:

dict = {
  "country" : "India",
  "continent" : "Asia",
  "Other_name" : "Bharat"
}
x = dict["continent"]
print(x)

is not correct the way I am thinking??

You are assigning the dict value for the key continent to x

How would you change the value for the key continent to “Europe” ?

It would be:

dict['continent'] = "Europe"

Now, how would you assign the key node in the dictionary distances to 0?

1 Like

Thank you @pkdvalis and at this point I will never make it…

# I think is this one but only works on my editor not to yours  :(
distances[node] = 0

Never quit, you’ll make it. All you have to do is be persistent.

This is correct. Try resetting the question you might have changed something else.

If it still doesn’t work, please share the full updated code

Indeed I am doing something wrong I should not after three weeks with you…

# this below run ok in my editor but not into yours
def shortest_path(graph, start):
    unvisited = []
    distances = {}
    for node in graph:
     unvisited.append(node)
    if node == start:
       distances[node] = 0

I am getting the below erro r and trying and trying again and again :slight_smile: :
Sorry, your code does not pass. Keep trying.
You should create an if statement that executes when node is equal to start.

# is my if statement wrong..Personally, I do not think so. 
    if node == start:

If you think you won’t make mistakes after studying programming for 3 weeks then your expectations were very, very wrong.

You have some inconsistent indentations here:

The if statement is part of the for loop and should also be indented.

def function():
    code
    code
    for this in that:
        code
        code
        if this == that:
            code
            code

Dear @pkdvalis ,
now the code pass… It took more than 24 hours me to solve this step…no good no at all…I am not happy with myself.
Thanks

You kept at it, you didn’t give up. You solved it and most importantly you learned something. These are all great and worthwhile accomplishments!

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