Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm doing the famous game "Tower of Hanoi" and i have an error when i run my code. So, when i run it raise an IndexError: list index out of range, in line 17, which is " if new_disk <= self.content[0]:" in the add_method.
I saw that an Indexerror can be raised if the value aren't in the list or out of the range, but i don't know what's the problem in my case. Here's my code:

What I have tried:

class HanoiT():

    def __init__(self, content=None):
        if content is None:
            self.content=[]
        else:
            self.content=content

    def addmethod(self, new_disk): #Add an element on the beginning of the list
        if new_disk <= self.content[0]:
            self.content.insert(0, new_disk)
        else:
            raise ValueError("No disk may be placed on top of a smaller disk")


    def deletemethod(self): #Delete the first element of a list
        self.content.pop(0)

    def affichage(self): #Method to print the list which represent the "disk" putted on the tower from the top to bottom
        for i in self.content:
            print(i, end=" ")

    def movedisk(self, Tsource, Ttarget): 
        Ttarget.addmethod(Tsource.content[0])
        Tsource.deletemethod()

    def movetower(self, N, Tour1, Tour3, Tour2):
        if N >= 1:
            self.movetower(N-1, Tour1, Tour2, Tour3)
            self.movedisk(Tour1, Tour3)
            self.movetower(N-1, Tour2, Tour3, Tour1)




N = 4
Tour1 = HanoiT([x for x in range(1, N+1)])
Tour1.affichage()
Tour2 = HanoiT([])
Tour3 = HanoiT([])
h = HanoiT()
h.movetower(N, Tour1, Tour2, Tour3)
Posted
Updated 30-Oct-17 11:02am
Comments
Richard MacCutchan 30-Oct-17 14:02pm    
Because self.content does not contain anything.

If you get an "Index out of range" error, it means that you have tried to access an array or other collection, but the index number you tried to fetch is not in the collection - it's either negative, or larger than the number of elements in the collection minus one (because indexes start at zero, so if you have two elements in your collection they are indexed via 0 and 1 respectively).

In this case, you are only trying to access one collection with that line: self.content and you are supplying a fixed index of zero - so the collection can have no elements.
Why not? Probably because the collection has not had any elements added to it yet, so there is nothing you can compare the value of.

Solution: change the if condition to check if the collection is empty before you try and use the value.
 
Share this answer
 
Comments
Member 13494159 31-Oct-17 13:42pm    
Thank you man, i manage to correct it !
OriginalGriff 31-Oct-17 15:01pm    
You're welcome!
Quote:
but i don't know what's the problem in my case.

Use the debugger to see what your code is doing and inspect variables at point of failure.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Member 13494159 31-Oct-17 13:42pm    
Thanks, i used a print in functions to see where i was, and i found my error
Patrice T 31-Oct-17 15:03pm    
Nice to hear it. Give a try to debugger, it is really helpful.

If a solution was helpful, accept it, it will tell everyone that the question is solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900