0

im new in programming, i was trying simple code rle decompress, but im get error 'int' object is not subscriptable in if (JumlahKarakter[i].isalpha() == True): , how to fix this error?

this my code

TeksAsli = input("Masukan Input Teks Yan Akan Dikompress: ")
JumlahKarakter = len(TeksAsli)
Teks = ""
for i in range(0, JumlahKarakter):
    if (JumlahKarakter[i].isalpha() == True):
        for j in range(0,JumlahKarakter[i+1]):
            Teks = Teks + TeksAsli[i]

print("Hasil Decompress = ", Teks)
  • Don't you mean `TeksAsli[i]` and not `JumlahKarakter[i]`? `TeksAsli` is a string, which is subscriptable, but you're trying to subscript `JumlahKarakter`, which is just a number, since it's from `len(TeksAsli)`. – Random Davis Mar 15 '22 at 17:13

3 Answers3

0

What you are basically trying to do in this code is you are subscripting the length of the input you have taken (i.e len(TeksAsli) which is stored in TeksAsli JumlahKarakter). This will surely return an error because for example if the length is 1. You are doing something like 1[1] which makes no sense. Secondly, in python, you do not require () parentheses to define if statement conditions. Third, you do not need to check in an if statement if the value is a bool, because if it is true it will automatically run the if statement. Here's what you can do:

TeksAsli = input("Masukan Input Teks Yan Akan Dikompress: ")
JumlahKarakter = len(TeksAsli)
Teks = ""
for i in range(0, JumlahKarakter):
    if TeksAsli[i].isalpha():
        Teks += TeksAsli[i]

print("Hasil Decompress = ", Teks)

I've removed some small errors or not needed code to improvise it. However, this above code is correct but there is a more efficient way of doing it which is:

TeksAsli = input("Masukan Input Teks Yan Akan Dikompress: ")
final = ''.join(TeksAsli.split(' ')) #Splits the string into list wherever space found. Then joins it without any spaces
print(final)

Both the programs work perfect, but the second one is more efficient and doesn't require a lot of variables.

The Myth
  • 1,090
  • 1
  • 4
  • 16
0

Since len() returns an integer, stopping value of the range() needs to be JumlahKarakter instead.
I mean

for j in range(0,JumlahKarakter[i+1]): 

This needs to be like this

for j in range(0,JumlahKarakter):

And the same goes to if statement.

Btw, Looks like you're trying to do something like this

text = input()
alpha = []
nums = []
for i in text:
    if i.isalpha():
        alpha.append(i)
    else:
        nums.append(i)
for j in range(len(alpha)):
    print(alpha[j]*int(nums[j]),end="")

You can also do it like this

text = input()
for i in range(0,len(text),2):
    print(text[i]*int(text[i+1]),end="")
Python learner
  • 1,159
  • 1
  • 8
  • 20
0
import re

TeksAsli = input("Masukan Input Teks Yan Akan Dikompress: ")
try:
    find_num = int(re.findall(r'\d+', TeksAsli)[0])
    find_text = re.sub("(\d+)", "", TeksAsli)
    Teks = ""
    for i in range(find_num):
        Teks += find_text
    print(Teks)
except IndexError:
    print("Error")
  • that's program work, but if im remove 'i+1' the program output is just **BB** if im input **B5**, i wanna trying decompres, if im input **B5**, output is **BBBBB**. – Ahlul Mukhramin Mar 15 '22 at 17:37
  • I changed the code, can you try? as you want – Tolga ÇAĞLAYAN Mar 15 '22 at 17:54
  • thanks, program it's work. I wanna asking again. Why when im input **B10** the output just **B** not **BBBBBBBBBB** – Ahlul Mukhramin Mar 15 '22 at 20:09
  • Sorry forgot.. I tested it now and it works – Tolga ÇAĞLAYAN Mar 15 '22 at 21:14
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 16 '22 at 00:09