freeCodeCamp.org

for node, distance in graph[current]:
            pass
       if distance + distances[current] < distances[node]:
             pass

I am not able to indent this code. Please, can you share with me a link where I can study the indentation in Python?
Thank you

you can read about indentation in python here: Indentation in Python - Rules of Indentation in Python
or here: Indentation in Python - GeeksforGeeks

1 Like

Thank you @ilenia
I have always the same issue…The code run ok in my editor but doesn’t pass in yours. I use the 4 spaces.
In the code aboce we have two blocks of code, right? the 1 block is for node, and the 2 block is if distance, right??

please share all the code in your editor

while unvisited:
        current = min(unvisited, key=distances.get)
    for node, distance in graph[current]:
            pass
    if distance + distances[current] < distances[node]:
     pass

    print(f'Unvisited: {unvisited}\nDistances: {distances}\nPaths: {paths}')
    
shortest_path(my_graph, 'A')

Thank you

please, all the code. This is invalid code, I can’t call a function that is not defined, and shortest_path is never defined in your code

Sorry …here the full code:

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

def shortest_path(graph, start):
    unvisited = list(graph)
    distances = {node: 0 if node == start else float('inf') for node in graph}
    paths = {node: [] for node in graph}
    paths[start].append(start)
    
    while unvisited:
        current = min(unvisited, key=distances.get)
    
    for node, distance in graph[current]:
            pass
    if distance + distances[current] < distances[node]:
     pass

    print(f'Unvisited: {unvisited}\nDistances: {distances}\nPaths: {paths}')
    
# shortest_path(my_graph, 'A')

I would suggest you hit the reset button at the bottom. You have modified the original code in parts that it should have not been touched.
The request expects that you remove the pass keyword (originally given) and substitute it for the if condition, and as the block of the if for now, write again the word pass with proper indentation.

@anon28508191 it works…Thank you so much…

As you said I deleted the first pass. Actually I deleted it many time while I was trying but did not work.
Stupid question: why the first pass should be removed? We have here two blocks: one for the for loop and another for the if loop : I thought that the two pass were necessary based on the definition of pass statement given by W3schools:

The pass statement is used as a placeholder for future code . When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed. Empty code is not allowed in loops, function definitions, class definitions, or in if statements.

Thank you

Initially you have this, which it will be a loop block.

But Python does not allow you to have an empty block, and that’s why it had the keyword pass indented.
When the task asked you to create a condition with the keyword if as part of the loop block, that code becomes what the placeholder for future code was waiting for, or keeping the place for.
From the point of view of Python, it is not going to complain if you leave it but it doesn’t have any purpose once that the if condition is written, and it fails the validation test.
Notice that the if itself becomes a block, and you needed the pass again until more code would be placed under its indentation.

Thank you for your very useful explanation @anon28508191 and Thank you to @ilenia as well.