Python Maze Explorer: Calling .explore() method with "NameError: name current_room is not defined"

Currently doing the Maze Explorer for Complex Data Structures course: https://www.codecademy.com/courses/complex-data-structures/projects/maze-explorer

Within Script.py, I am calling the .explore() method like this:

from graph import Graph, build_graph
from vertex import Vertex

excavation_site = build_graph()
excavation_site.explore()

Then, in my While Loop, I have:

while current_room !='treasure room':
  node = self.graph_dict[current_room]

  for connected_room, weight in node.edges.items():
    key = connected_room[:1]
    print("enter {0} for {1}: {2} cost".format(key, connected_room, weight))

  valid_choices = [connected_room[:1] for connected_room in node.edges.keys()]

  print("\nYou have accumulated: {0} cost".format(path_total))

  choice = input("\nWhich room do you move to? ")

Within graph.py, here is the .explore() method:

  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))
    return current_room

Traceback (most recent call last):

  • File “script.py”, line 8, in *
  • while current_room !=‘treasure room’:*
    NameError: name ‘current_room’ is not defined

How is current_room passed to through the Graph class? I’ve tried a few different things but cannot get this to work. Any help is much appreciated!

I have not done the project so not sure, but does current_room need to have a default definition?

such as “current_room = ‘storage room’”

Hi, it’s defined in .explore(). I attempted moving it out of the defintion as well and still getting the same error.

I turns out that I had the While loop in the Script.py but it needs to be in the Graph.py within the .Explore() method. This resolves the issue!