Unknown Python Bug | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Unknown Python Bug

I am trying to implement a code for solving tower of hanoi. So far i have implemented just the functions for moving the disks.However i have encoutered a bug. Here's the code https://code.sololearn.com/cwGYE3i7v3TZ/?ref=app The code ouputs: [[1, 2, 3], [None, None, None], [None, None, None]] [[None, 2, 3], [None, None, 1], [None, None, 1]] /\ | I can't figure out how does disk1 reaches here (tower2 )when i have coded it to place the disk in tower 3. I have tried some other methods but all of them lead to this I am using python3 and this bug appears on all my devices.

15th Jun 2019, 3:05 PM
Prabhakar Dev
Prabhakar Dev - avatar
2 Answers
+ 4
The error is in line 3. towers=3*[len(disks)*[None]] creates three instances of the same list. If you change one of them, you change all of them. You can create three independent lists with towers = [len(disks)*[None] for i in range(3)]
15th Jun 2019, 3:31 PM
Anna
Anna - avatar
+ 3
thank you
16th Jun 2019, 5:07 AM
Prabhakar Dev
Prabhakar Dev - avatar