The Student Room Group

Computing GCSE, Python

num = 1

user = str(input("Input a series of letters to be run length encoded: "))

for i in range(0, len(user)):
if user == user[i + 1]:
num = num + 1
else:
print(num, user)
i = i + 1

This code above is a code for run length encoding however it gives me a string index out of range error, however if I add -1 at the end of Len(user) it won't print out the last letters of the list. And help?!
Reply 1
The error occurs from

if user == user[i + 1]:

Since you check user[i+1], which will be out of range for i=len(user)-1

Quick Reply

Latest

Trending

Trending