Reputation: 1
when I input A15, it produces "IndexError: string index out of range" on line 7. what is the cause of my code error ?
Compress = input("Silahkan Input teks: ")
HasilDecompress =""
PKarakter =len(Compress)
for i in range (0, PKarakter ,2):
JumPerulangan = int(Compress[i+1])
for j in range (0, JumPerulangan):
HasilDecompress = HasilDecompress + Compress [i]
print ("Hasil Decompress Adalah = ", HasilDecompress)
Upvotes: 0
Views: 126
Reputation: 135
You have to decrease the PKarakter
by 1 because you access Compress[i+1]
. So that makes it out of range.
So you have to change:
PKarakter = len(Compress)-1
Upvotes: 0