Eliminate the source of the faulty index.Figure out the faulty index just before the error is thrown.But the general debugging strategy remains even for advanced code projects: Note that this is a minimal example and it doesn’t make a lot of sense. The correct code is, therefore: # CORRECT CODE The final one is the index i=4 which points to the fifth element in the list (remember zero-based indexing: Python starts indexing at index 0!).īut the list has only four elements, so you need to reduce the number of indices you’re iterating over. You can now see all indices used to retrieve an element. The result of this code snippet is still an error.įile "C:\Users\xcent\Desktop\code.py", line 6, in So, let’s insert a print statement before that line: lst = The error message tells you that the error appears in line 5. # File "C:\Users\xcent\Desktop\code.py", line 5, in Here’s an example of wrong code that will cause the error to appear: # WRONG CODE ![]() This way, you’ll have your wrong index in the shell right before the error message. To achieve this, you can print the index that causes the error before you use it on the list. To pin down the exact problem, check the value of the index just before the error occurs. So, how can you fix the code? Python tells you in which line and on which list the error occurs.
0 Comments
Leave a Reply. |