Lomore Lomore - 2 months ago 14
Python Question

python 'string index out of range' error

I'm trying to code a word scrambler but when I try to append letters from my word, using index, I get the error 'String index out of range'. I have tried this without the 'input' but once I added it in I started getting problems.
my code is:

a = input('word ->')
b = []
count = 0
while count < 5:
b.append(a[count])
count +=1
print(b)


it would be great if someone could help. thanks

Answer

Because when you give input smaller than 5 a[count] is out of index. So try this one:

a = input('word ->')
b = []
count = 0
while count < len(a):
    b.append(a[count])
    count +=1
print(b)
Comments