Click here to Skip to main content
15,113,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
f = open (pdbID,'r')
k = 0

data = f.readlines()
g = open (dump, 'w')
for linedata in data:
    line=linedata.strip()
    words = line.split()
    if (words[12] == '4'):
        k=k+1

    g.write(linedata)

if (words[12] == '4'):
IndexError: list index out of range

What I have tried:

I am stuck at this part of the code and I can not move forward
Posted
Updated 8-Aug-21 21:50pm
Comments
Richard MacCutchan 7-Oct-18 3:14am
   
Do not assume the number of elements in your array, check first how many there are.

1 solution

Quote:
IndexError: list index out of range

Make sure you have at least 12 items in array words.
   
Comments
Richard Deeming 9-Oct-18 13:26pm
   
Arrays in Python, as in any sane language, are zero-based; so you'll need to have at least 13 items in the array for words[12] to work. :)
Patrice T 9-Oct-18 14:23pm
   
:) I am not a python user.
"as in any sane language"
Almost any, but not all.

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