so I'm struggling to complete a coding challenge in CodeWars, and I received an IndexError on CodeWars. When I run the code on PyCharm, the output is fine.
Coding Prompt
def alphabet_position(sentence):
alphabet_cap = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
alphabet_low = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
sentence_list = list(sentence)
# remove any non-letters from sentence
i = 0
while i < len(sentence_list):
if sentence_list[i].isalpha() == False:
sentence_list.remove(sentence_list[i])
i += 1
# I had to create a specific remove(" ") because for some reason the "'" would be turned into a " " instead of being removed?
i = 0
while i < len(sentence_list):
if sentence_list[i] == " ":
sentence_list.remove(" ")
i += 1
# finding position of alphabet
alpha_index = []
k = 0
j = 0
while k < len(sentence_list):
if sentence_list[k] == alphabet_low[j] or sentence_list[k] == alphabet_cap[j]:
alpha_index.append(str((alphabet_low.index(alphabet_low[j])) + 1))
j = 0
k += 1
else:
j += 1
sentence_index = " ".join(alpha_index)
return sentence_index
Error message
Sample Tests:
from random import randint
test.assert_equals(alphabet_position("The sunset sets at twelve o' clock."), "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11")
test.assert_equals(alphabet_position("The narwhal bacons at midnight."), "20 8 5 14 1 18 23 8 1 12 2 1 3 15 14 19 1 20 13 9 4 14 9 7 8 20")
number_test = ""
for item in range(10):
number_test += str(randint(1, 9))
test.assert_equals(alphabet_position(number_test), "")```
Any help would be appreciated!