Towers of Hanoi issue


#1

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

Getting this error:
Traceback (most recent call last):
File “script.py”, line 53, in
if from_stack.is_empty()== True:
AttributeError: ‘str’ object has no attribute ‘is_empty’

#Play the Game
num_user_moves=0
while right_stack.get_size()!=num_disk:
print("\n\n\n…Current Stacks…")
for item in stacks:
print(item.print_items())
while True:
print("\nWhich stack do you want to move from?\n")
from_stack=get_input()
print("\nWhich stack do you want to move to?\n")
to_stack=get_input()
if from_stack.is_empty()== True:
print ("\n\LnInvalid Move. Try Again")
elif to_stack.is_empty()==True or from_stack.peek()<to_stack.peek():
disk=from_stack.pop()
to_stack.push(disk)
num_user_moves+=1
break
else:
print ("\n\nInvalid Move. Try Again")
print("\n\nYou completed the game in {} moves, and the optimal number of moves is {}".format(num_user_moves,num_optimal_moves))


#2

Just in case, this is my entire code:

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_stack)
stacks.append(middle_stack)
stacks.append(right_stack)

#Set up the Game
num_disk=int(input("\nHow many disks do you want to play with?"))
while num_disk<3:
num_disk=int(input(“Enter a number greater than or equal to 3\n”))
for i in range(num_disk,0,-1):
left_stack.push(i)
num_optimal_moves=2**left_stack.get_size() - 1
print("\nThe fastest you can solve this game is in {} moves".format(num_optimal_moves))
#Get User Input
print(num_optimal_moves)
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]
return “enter {} for {}”.format(letter,name)
user_input=input("")
if user_input in choices:
for i in range(0,len(stacks)):
if user_input==choices[i]:
return stacks[i]
print(get_input())

#Play the Game
num_user_moves=0
while right_stack.get_size()!=num_disk:
print("\n\n\n…Current Stacks…")
for item in stacks:
print(item.print_items())
while True:
print("\nWhich stack do you want to move from?\n")
from_stack=get_input()
print("\nWhich stack do you want to move to?\n")
to_stack=get_input()
if from_stack.is_empty()== True:
print ("\n\LnInvalid Move. Try Again")
elif to_stack.is_empty()==True or from_stack.peek()<to_stack.peek():
disk=from_stack.pop()
to_stack.push(disk)
num_user_moves+=1
break
else:
print ("\n\nInvalid Move. Try Again")
print("\n\nYou completed the game in {} moves, and the optimal number of moves is {}".format(num_user_moves,num_optimal_moves))


#3

Hi @focussion,

Your code is not formatted, which makes it difficult to understand. Its indentation, which is essential for defining the structure of a Python program, is not visible.

Why do you have this as a return statement in the get_input() function? …

      return "enter {} for {}".format(letter,name)

As a result of the above, the function returns a str object instead of a reference to a Stack. It should be a print statement instead.