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


#1

Hello again. it is look like correct?
30.If the user moves a disk to an empty stack or moves a disk onto a larger disk, that’s a valid move.
In the inner while loop, create an elif statement that checks if EITHER of the following is True :

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_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 i in range(num_disks,0,-1):
  left_stack.push(i)
num_optimal_moves = 2**num_disks-1
print("\nThe fastest you can solve this game is in {} 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 {} for {}".format(letter, name))
    user_input = input("")
    if user_input is 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 Sacks...")
  while True:
    print("\nWich stack do you want to move from?\n")
    from_stack = get_input()
    print("\nWich 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()
      to_stack.push(disk)
      num_user_moves +=1
      break
    else:
      print("\n\nInvalid Move. Try Again")
  for i in stacks:
    i.print_items()
print("\n\nYou completed the game in {} moves, and the optimal number of moves is {}".format(num_user_moves, num_optimal_moves))

How I can compare value of disks? what attribute or method I should use(peek)? : The “peeked” value of the from_stack is less than the “peeked” of the to_stack


#2

Hi @ihorivanii,

What is the purpose of this statement in the get_input() function, and does it serve that purpose correctly?

    if user_input is choices:

Yes, use the peek() method, as you are doing.