Learn Recursion by Solving the Tower of Hanoi Puzzle - Step 3

Tell us what’s happening:

I already solved the problem, but why is h negative here? I am just curious about this.

The problem involves counting down from 3 to 1. According to the instructions, range(x, y, h) is a function where x is the initial integer and that y is the final one (not inclusive). However, why is h negative if 3-2 is 1 (not negative one)?

The next number after 3 is 2. So, why does this exercise treat h as if it is 2-3 = -1?

Your code so far


# User Editable Region

rods = {
    'A': [],
    'B': [],
    'C': []
}

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Learn Recursion by Solving the Tower of Hanoi Puzzle - Step 3

The range function takes three parameters:

for i in range(5, 0, -1):
    print(i)

# 5 4 3 2 1

The third parameter is the step, which defines the incrementation. Default is 1, which will positively increment by one. By contrast, -1 will negatively increment by one.

1 Like

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