Towers of Hanoi https://www.codecademy.com/paths/computer-science/tracks/linear-data-structures/modules/cspath-stacks/projects/towers-of-hanoi

Hey Codecademy community,
I am currently working on the Towers of Hanoi project within the Computer Science course.
I am on Step 20 and I got this error message when running the code in the terminal:
AttributeError: ‘str’ object has no attribute ‘get_name’

I don’t understand why I got this error message because I looked at the hint and I watched the get help video and I have the same exact code.

Here is my code:
image

Is there something that I am missing?
Thanks,
Nava

Hello, @mr.carrotorangepickl.

If you could post your actual code (make sure to retain its original formatting by using the preformatted text button </>) rather than a screen shot, someone may be able to help.

Hi Midlindner
Here is my code. Thanks!!

from stack import Stack

print("\nLet's play Towers of Hanoi!!")

#Create the Stacks
stacks = [] 
left_stack = Stack("Left")
middle_stack = Stack("Middle")
right_stack = Stack("Right")
stacks.append("Left")
stacks.append("Middle")
stacks.append("Right")


#Set up the Game
num_disks = int(input("\nHow many disks do you want to play with?\n"))
while num_disks < 3:
  num_disks = int(input("Enter a number greater than or equal to 3\n"))
  for disk in range(num_disks, 0, -1):
    left_stack.push(disk)
    num_optimal_moves = {0}
    print("\nThe fastest you can solve this game is in {0} moves")
   




#Get User Input
def get_input():

  choices = [stack.get_name()[0] for stack in stacks]
  while True:
    
    for i in range(len(stacks)):
      name = stacks[i].get_name()
      letter = choices[i]
      print("Enter {0} for {1}")
    user_input = ""

        
#Play the Game

You have a list called stacks. Presumably, it should contain instances of the Stack class. Those Stack objects have a method named get_name. The error is telling you that strings do not have a property named get_name. What does your list, stacks, contain? Stack instances or strings?

1 Like

So by removing the .get_name from my stack my code should work?

No. You need to have a list called stacks that is populated with Stack objects (instances). What you currently have is a list of strings: ["Left", "Middle", "Right"]

You created three Stack objects:

Those objects should be in the list not the strings.

1 Like