Answer the question
In order to leave comments, you need to log in
Why does an error occur when accessing an array element?
I am new to python, wanted to write a simple cipher and got an error:
IndexError: list index out of range
Code:
def cipher(text):
alphabet = list('abcdefghijklmnopqrstuvwxyz')
list_text = list(text)
result_text = ''
for el in list_text:
change_index = alphabet.index(el) + 3
if change_index <= len(alphabet):
result_text += alphabet[change_index]
else:
change_index = (alphabet.index(el) + 3) - len(alphabet)
result_text += alphabet[change_index]
return result_text
print(cipher('xyz'))
Answer the question
In order to leave comments, you need to log in
I am new to python
if change_index <= len(alphabet):
result_text += alphabet[change_index]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question