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

Tell us what’s happening:

Can anyone help me please? I’ve been trying for sooo long ;-; I don’t get it

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 = []
    distances = {}
    for node in graph:
        unvisited.append(node)
        if node == start:
            distances[node] = 0
        else:
            distances[node] = float('inf')
    print(f'Unvisited: {unvisited}\nDistances: {distances}')

def shortest_path(my_graph, 'A'):
    print(shortest_path(my_graph, 'A'))

# 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/122.0.0.0 Safari/537.36

Challenge Information:

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

This defines a function, which you have already done. To call a function you just do this:

print()

To call the print function for example. Arguments go in the parentheses.

print(a,b)

I called print and passed the arguments a and b

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 = []
    distances = {}
    for node in graph:
        unvisited.append(node)
        if node == start:
            distances[node] = 0
        else:
            distances[node] = float('inf')
    print(f'Unvisited: {unvisited}\nDistances: {distances}')
    def shortest_path(my_graph, 'A'):
        print(my_graph, 'A')

I’ve tried so many times but it keeps saying: " You should call shortest_path passing my_graph and 'A' as the arguments." Am I missing something? ;-;

@Hi @AllieN
Much better, thank you for correcting the format of the code so we can judge the space which for Python is very critical. Already is showing something unusual, you are declaring twice the same function (with def inside), one inside the other.

Perhaps a little example might help.
This is a function declaration:

def showme(text):
    print(text)

Now, if we want to call it, once it has been defined, this is how we call it.

showme("some text here")

That’s the difference that you need to apply in this issue.

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 = []
    distances = {}
    for node in graph:
        unvisited.append(node)
        if node == start:
            distances[node] = 0
        else:
            distances[node] = float('inf')
    print(f'Unvisited: {unvisited}\nDistances: {distances}')
def shortest_path(my_graph, 'A'):
    shortest_path("my_graph, 'A'")

I tried following the example but it keeps wrong, is there anything else I should apply?

You have this:

def shortest_path(..., ...):
    ...
    ...
    ...
    ...

Now the way to call it, as I showed you before it would be?

shortest_path(something_here, something_else)

Compare with what you are doing.

This should only appear once in your code. You define a function only once.

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(my_graph, 'A'):
    shortest_path("my_graph, 'A'")
    unvisited = []
    distances = {}
    for node in my_graph:
        unvisited.append(node)
        if node == 'A':
            distances[node] = 0
        else:
            distances[node] = float('inf')
    print(f'Unvisited: {unvisited}\nDistances: {distances}')

I’m still stuck… I’ve already tried many things but it’s still wrong… I’ve already deleted the other function I’ve created but I can’t find the right position to place the shortest_path(“my_graph, ‘A’”)

write it right after the def block. Pay attention to the white space. The def block ends with the indented print().
Give it an empty line and start writing at the same level that the word def.

on a new line at the end of your code, after the function definition

You should also reset the step, because you’ve changed the function definition now.

It works!! Thank y’all very much! :DDD

1 Like

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