Towers of Hanoi

Hi, everybody I’m new in programming, so I was follow the video and I don’t know why I have this error, please some one could help me?

This is the 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 += [left_stack, middle_stack, right_stack]

#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):
num_optimal_moves = 2 ** num_disks-1
print("\nThe fastest you can solve this game is in {0} moves".format(num_optimal_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}".format(letter, name ))
    user_input = input("")
    if user_input in choices:
      for i in range(len(stacks)):
        if user_input == choices[i]:
          return stacks[i]

#Play the Game

num_user_moves = 0
while right_stack.get_size() != num_disks:
  print("\n\n\n...Current Stacks...")
  for stack in stacks:
  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.get_size == 0:
      print("\n\nInvalid Move. Try Again")

    elif to_stack.get_size == 0 or from_stack.peek()< to_stack.peek():
      disk = from_stack.pop()
      num_user_moves += 1
      print("\n\nInvalid Move. Try Again")

print("\n\nYou completed the game in {0} moves, and the optimal number of moves is {1}".format(num_user_moves, num_optimal_moves))

This is the message error:
Traceback (most recent call last):
File “”, line 64, in
elif to_stack.get_size() == 0 or (from_stack.peel() < to_stack.peek()):
TypeError: ‘<’ not supported between instances of ‘int’ and ‘NoneType’

Hello, @migsotoa, and welcome to the forums.

Look closely at the line referenced in the error. It’s telling you that you can’t use < to compare an int to a value of NoneType. That is very true, but the mistake precedes that comparison. Were it not for the mistake, that comparison would not occur, so you need to look at the value that precedes the or. If it seems correct, you can try printing it out prior to using it to see what it is.

Thank you, I’m going to check

Thank you, is working now…
IThank you, is working now…
I want to ask you, if you could give me some advice, this is because I felt very hard the topics of nodes and stacks, what could you tell me about what kind of exercises I could look for , or what extra material I could read?

1 Like

Sorry, I don’t have any recommendations for you. Someone else may, but all I could suggest is Google. I do have a suggestion regarding this project, however. Recall that the Stack class has a method is_empty():

  def is_empty(self):
    return self.size == 0

This could be used instead of .get_size() == 0.

Thank you very much.

1 Like

stack is just like a stack of plates

you can add or remove at the top of the stack, if you want to add or remove further down, you have to pop things off the top first

linked list:

Thank you for share the link :+1:

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