1 Replies - 2182 Views - Last Post: 24 October 2013 - 10:21 AM Rate Topic: -----

#1 TrueFan22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 14-September 13

Python the towers of hanoi

Posted 24 October 2013 - 10:13 AM

My code is not printing out at all.
I m new to python

def hanoi(n, source, helper, target):
    if n > 0:
        # move tower of size n - 1 to helper:
        hanoi(n - 1, source, target, helper)
        # move disk from source peg to target peg
        if source:
            target.append(source.pop())
        # move tower of size n-1 from helper to target
        hanoi(n - 1, helper, source, target)
        
if __name__ == "__main__":
        
source = [4,3,2,1]
target = []
helper = []
hanoi(len(source),source,helper,target)

print source, helper, target





Is This A Good Question/Topic? 0
  • +

Replies To: Python the towers of hanoi

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,532
  • Joined: 12-December 12

Re: Python the towers of hanoi

Posted 24 October 2013 - 10:21 AM

You are not new to this forum though. How did you manage to post in the Java forum? :dontgetit:

You have an indentation error.

if __name__ == "__main__":  
    source = [4,3,2,1]
    target = []
    helper = []
    hanoi(len(source),source,helper,target)

    print (source, helper, target)

Correct indentation is essential in Python.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1