H
H
HelvegenFox2018-10-27 21:48:22
Python
HelvegenFox, 2018-10-27 21:48:22

Why does an index error occur when accessing a row?

Good day to all.
I am solving a problem in Python to decode a string.

s_zip = "r12t1x1N15P5i17L2N2u4D8e18V1N1e1N14f1D4H10k2Y19n8R18T13a6B14G18e10L6k20q3l1"
s_out = ""
print(s_zip)
part_of_string = ""
count_str = ""
count = 0
for i in range(0, len(s_zip)):
    j = i+1
    if s_zip[i].isalpha():
        part_of_string = s_zip[i]
    if s_zip[i].isdigit() and s_zip[j].isalpha():
        count_str += s_zip[i]
        count = int(count_str)
        while count < 0:
            s_out += part_of_string
            count -= 1
        count_str = ""
    if s_zip[i].isdigit() and s_zip[j].isdigit():
        count_str += s_zip[i]
print(s_out)

At startup, it swears at s_zip[j].isalpha() with the error IndexError: string index out of range
Help me understand the reason, please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sash999, 2018-10-29
@HelvegenFox

At the last iteration, you get out of the line, which is what they tell you in plain text.
Let's say the string length is 10. The last iteration is i=9. And j=i+1, that is 10. And your construction s_zip[j] causes an error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question