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```
3 Answers
+ 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).
+ 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.
+ 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.