0

List index out of range.

Why does this return list index out of range on l=block [p]? ```def encode (m): m=m.split() msg=[] for block in m: ltrs=[] for p in range(0,7): l=block[p] ltrs.append(enc(l,p)) ltrs=''.join(ltrs) msg.append(ltrs) msg=' '.join(msg) return msg```

26th Nov 2019, 4:42 AM
August Borne
August Borne - avatar
3 Respostas
+ 6
Do you understand what the function is supposed to do? It may error if you give it too short words as parameter (less than 7 characters).
26th Nov 2019, 5:10 AM
Tibor Santa
Tibor Santa - avatar
+ 6
If not all your strings are 7 character long, then range(0,7) is not good. Try like this: for index, char in enumerate(block): ltrs.append(enc(char, index)) enumerate is great if you want to track the position AND the value of each element in a collection at the same time.
26th Nov 2019, 5:58 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Tibor Santa if you see the updated code above, I just wanted to cut out the extra bits at first. The problem actually is string index not list index I didn't realize at first.
26th Nov 2019, 5:36 AM
August Borne
August Borne - avatar