There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
grand_central.add_edge(forty_second_street.value)
Why in the practice, it is add a ‘value’ instead of a vertex?
I thought add_edge method are supported to connect each vertex, so the argument should be a vertex. So I wrote
And the example showed in the instruction grand_central.add_edge(forty_second_street) also add a vertex instead of a value. so why in the practice, it become add a vertex’s value?
Could anyone explain this to me?
I know that this comes a months later, but since i just stumbled with this lesson i thought it would be worth to reply:
In the practice, we are adding the value of another vertex, maybe for readability purposes. You can add the vertex object but when you print you won’t get the output in string format (need to rely to vertex.value.
self.edges[vertex.value] = True
This will work if you add a vertex and not the value, but you have to put in the print statement print('Adding edge to {0}'.format(vertex.value))
In both cases we will add the value of the vertex.
There is no difference between what you are proposing and what the lesson explains.
adding the vertex itself is a viable solution to map the edges.
however when accessing the edges later it will return <vertex object at 432958423xo4> instead of the name of the vertex that is connected.
i believe adding “.value” to the argument every time you call “.add_edge()” is cumbersome and dont like codecademys solution. Here is what i believe to be a more elegant approach:
def add_edge(self, vertex):
print("Adding edge to " + str(vertex.value)) #prints the vertex's value being added
self.edges[vertex.value] = True #adds the vertex value to our dictionary of edges
This task made me realise that I don’t understand how the init constructor works.
What is self.value = value doing?
Is it saying that if I run it on an object, then a .value attribute is assigned to this object and the object’s “value attribute” will now be the same as the “value”?
If that is the case, then when:
grand_central.add_edge(forty_second_street.value)
is called, is it the same as writing:
grand_central.add_edge("42nd Street Station")
because both refer to the “42nd Street Station” vertex?
This is the code I use for the task:
class Vertex:
def __init__(self, value):
self.value = value
self.edges = {}
# define .add_edge() here
def add_edge(self, vertex):
print("Adding edge to " + vertex)
self.edges[vertex] = True
def get_edges(self):
return list(self.edges.keys())
grand_central = Vertex('Grand Central Station')
forty_second_street = Vertex('42nd Street Station')
print(grand_central.get_edges())
# call .add_edge() below here
grand_central.add_edge(forty_second_street.value)
print(grand_central.get_edges())
In the Vertex class self.edges is created as a dictionary, self.edges = {}. Using self.edges[vertex] = True creates a new key-value pair where the argument passed to vertex is the key and True is the value.
# Similar dictionary usage-
test_dict = {"a" : 0}
test_dict["b"] = False # this just adds a new key-value pair to test_dict
print(test_dict)
Out: {'a': 0, 'b': False}
In the given example you can check what’s stored as follows-
print(grand_central.edges)
Out: {'42nd Street Station': True}
Question: shouldn’t the print statement in .add_edges() be f"Adding edge to {self.value} rather than f"Adding edge to {vertex}"? Because the way I see it, the add_edge() method is adding an edge to self and not to the vertex that’s being added.