当我输入A15时,它产生"IndexError:字符串索引超出范围"在第7行。我的代码错误的原因是什么?
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)
您必须将PKarakter
减少1,因为您访问Compress[i+1]
。所以它超出了范围。
所以你必须改变:
PKarakter = len(Compress)-1