Interpret this error message

Below is the code I am working on. Error message pops up, and I don’t quite get the error message. Can anyone explain it to me? As far as I know “current_node” is a node and not a list.
URL is below.
https://www.codecademy.com/paths/computer-science/tracks/complex-data-structures/modules/cspath-trees/lessons/learn-trees-les/exercises/trees-v

# Define your "TreeNode" Python class below
class TreeNode:
  def __init__(self, value):
    self.value = value
    self.children = []

  def add_child(self, child_node):
    print("Adding " + child_node.value)
    self.children.append(child_node)
    
  def remove_child(self, child_node):
    print("Removing " + child_node.value + " from " + self.value)
    self.children = [child for child in self.children 
                     if child is not child_node]

  def traverse(self):
    nodes_to_visit = [self]
    while 0 < len(nodes_to_visit):
      #current_node, an object of class Node
      current_node = nodes_to_visit.pop()
      print("Traversing...")
      print(current_node.value)
      nodes_to_visit.append(current_node.children)
    
    

root = TreeNode("CEO")
first_child = TreeNode("Vice-President")
second_child = TreeNode("Head of Marketing")
third_child = TreeNode("Marketing Assistant")

root.add_child(first_child)
root.add_child(second_child)
second_child.add_child(third_child)

root.traverse()

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

we could print the current node to find out:

      print("Traversing...")
      print(current_node)
      print(current_node.value)

where we see that current_node is still a list. How could that be? We got the last element from nodes_to_visit.

Lets inspect the nodes we have to visit to find out:

      print(nodes_to_visit)
      current_node = nodes_to_visit.pop()

seems you created a nested list for some reason. That can’t be right?

when I print out “nodes_to_visit”, why does it print out an empty list when it has [self] in it?
And when I store it to some variable and print the it out, it prints out a memory location, if I am not mistaken?

i ran your code, nodes_to_visit is not empty?

no, that is the default representation (__repr__) of an object/class instance.

It is empty. But it still has [self] in it, and when pop() it and store it to a variable and print out the variable, it prints out repr.

So when I print the list [self] out directly, it is an empty list, and when I pop “self” and store it to some variable and print out the variable, it prints out repr. Why?

When I print out “nodes_to_visit” It is an empty list. But it still has [self] in it, and when pop() it and store it to a variable and print out the variable, it prints out repr . Why is it so?
URL is below.
https://www.codecademy.com/paths/computer-science/tracks/complex-data-structures/modules/cspath-trees/lessons/learn-trees-les/exercises/trees-v

# Define your "TreeNode" Python class below
class TreeNode:
  def __init__(self, value):
    self.value = value
    self.children = []

  def add_child(self, child_node):
    print("Adding " + child_node.value)
    self.children.append(child_node)
    
  def remove_child(self, child_node):
    print("Removing " + child_node.value + " from " + self.value)
    self.children = [child for child in self.children if child is not child_node]

  def traverse(self):
    nodes_to_visit = [self]
    while 0 < len(nodes_to_visit):
      #current_node, an object of class Node
      current_node = nodes_to_visit.pop()
      print(nodes_to_visit)
      #print("Traversing...")
      print(current_node)
      
      nodes_to_visit.append(current_node.children)
    
    

root = TreeNode("CEO")
first_child = TreeNode("Vice-President")
second_child = TreeNode("Head of Marketing")
third_child = TreeNode("Marketing Assistant")

#root.add_child(first_child)
#root.add_child(second_child)
#second_child.add_child(third_child)

root.traverse()

Order is important and the .pop method is used for a specific reason-
https://docs.python.org/3/tutorial/datastructures.html?highlight=pop

that’s not what you’d want to look at

you’d look at the thing that you’re trying to do something with which is crashing.
if you’re trying to look at some thing’s value attribute, then, that would be because you expect it to have a value attribute, why is that, what do you expect it to be, and, is it that?

if you try to bounce a ball and it doesn’t bounce, what is the problem? oh it’s a rock, those don’t bounce, you should probably get a ball instead.