Maze exployer project error

I am trying to complete the project on https://www.codecademy.com/paths/computer-science/tracks/complex-data-structures/modules/cspath-graphs/projects/maze-explorer

Here is my code for graph.py

from vertex import Vertex

class Graph:
  def __init__(self):
    self.graph_dict = {}

  def add_vertex(self, node):
    self.graph_dict[node.value] = node

  def add_edge(self, from_node, to_node, weight = 0):
    self.graph_dict[from_node.value].add_edge(to_node.value, weight)
    self.graph_dict[to_node.value].add_edge(from_node.value, weight)

  def explore(self):
    print("Exploring the graph....\n")
    #FILL IN EXPLORE METHOD BELOW
    current_room = "entrance"
    path_total = 0
    print("\nStarting off at the {0}\n".format(current_room))
    
    while current_room != "treasure room":
      node = self.graph_dict[current_room]

      for connected_room, weight in node.edges.items():
        key = connected_room[0]
        print("enter {0} for {1}: {2} cost".format(key, connected_room, weight))
      valid_choices = [character[0] for character in node.edges.key()]
      print("\nYou have accumulated: {0} cost".format(path_total))
      choices = input("\nWhich room do you move to?")


  def print_map(self):
    print("\nMAZE LAYOUT\n")
    for node_key in self.graph_dict:
      print("{0} connected to...".format(node_key))
      node = self.graph_dict[node_key]
      for adjacent_node, weight in node.edges.items():
        print("=> {0}: cost is {1}".format(adjacent_node, weight))
      print("")
    print("")

def build_graph():
  graph = Graph()
  # MAKE ROOMS INTO VERTICES BELOW...
  entrance = Vertex("entrance")
  ante_chamber = Vertex("ante_chamber")
  kings_room = Vertex("king's room")
  grand_gallery = Vertex("grand gallery")
  treasure_room = Vertex("treasure room")
  # ADD ROOMS TO GRAPH BELOW...
  graph.add_vertex(entrance)
  graph.add_vertex(ante_chamber)
  graph.add_vertex(kings_room)
  graph.add_vertex(grand_gallery)
  graph.add_vertex(treasure_room)
  # ADD EDGES BETWEEN ROOMS BELOW...
  entrance.add_edge(ante_chamber, 7)
  entrance.add_edge(kings_room, 3)
  kings_room.add_edge(ante_chamber, 1)
  grand_gallery.add_edge(ante_chamber, 2)
  grand_gallery.add_edge(kings_room, 2)
  treasure_room.add_edge(ante_chamber, 6)
  treasure_room.add_edge(grand_gallery, 4)
  # DON'T CHANGE THIS CODE
  graph.print_map()
  return graph

what is wrong here?

Error:

Traceback (most recent call last):
File “script.py”, line 6, in
excavation_site.explore()
File “/home/ccuser/workspace/maze-explorer/graph.py”, line 26, in explore
key = connected_room[0]
TypeError: ‘Vertex’ object does not support indexing

The error is coming from your explore function. Particularly in this section:

 while current_room != "treasure room":
      node = self.graph_dict[current_room]
      for connected_room, weight in node.edges.items():
        key = connected_room[0]
        print("enter {0} for {1}: {2} cost".format(key, connected_room, weight))

You should try to check out what node.edges.items() is exactly and what you need to set your key to for it to work as you want it to. That’s probably just the surface of the problem, but it’s a good starting point. You’ll have to trace it to the root. It may help to draw out a map of your code with specifications for the types you’re using at each juncture.

1 Like

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