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

Tell us what’s happening:

Hello, I have tried several ways to create the list for position ‘A’ using range, but it doesn’t want to work. could anyone help me?
rods = {
‘A’: [num for num in range(3, 0, -1)],
‘B’: ,
‘C’:
}
also, with list(range(3,0,-1)
and add the value after declarate variable.

Your code so far


# User Editable Region

rods = {
    'A': [num for num in range(3, 0, -1)],
    '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/122.0.0.0 Safari/537.36

Challenge Information:

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

This one is a bit tricky.

You have the correct syntax for range
You don’t need the list comprehension, maybe you already tried without that.

The key here is that storing the range creates an object that you can treat as a list. What you’ve done creates an actual list. What you’ve done isn’t really wrong, but one creates a list, and one creates a generator.

a=[num for num in range(3, 0, -1)]
print(a)

>>>[3, 2, 1]
a=[range(3, 0, -1)]
print(a)

>>>[range(3, 0, -1)]

It doesn’t work the same way. Now it’s effectively a list in a list.

a=range(3, 0, -1)
print(a)

>>>range(3, 0, -1)
# the range object is stored in the variable a 

for num in a:
  print(num)

>>>3
>>>2
>>>1
# and acts as a list when accessed

So you don’t need to make it a list using []