Edits: Updated 'hi' to 'list'
I am a beginner and I have been working on a project to translate English sentences into pig latin, but I have run into an issue while trying to remove the spaces before punctuation in the sentence. Here is the script I am having trouble with.
import string
list = ['H', 'i', 's', 't', 'a', 'y', ' ', 's', 'i', 'a', 'y', ' ', 'a', 'a', 'y', ' ', 'e', 's', 't', 't', 'a', 'y', ' ', '.', ' ', 'H', 'i', 's', 't', 'a', 'y', ' ', 's', 'i', 'a', 'y', ' ', 'a', 'a', 'y', ' ', 'e', 's', 't', 't', 'a', 'y']
h = 0
for h in range(len(list)):
if list[h] in string.whitespace:
if list[h + 1] in string.punctuation:
list.pop(h)
h = h + 1
else:
h = h + 1
else:
h = h + 1
print(list)
When I run it I get the error:
File " ... ", line 110, in <module>
if list[h] in string.whitespace:
IndexError: list index out of range
When I print Hi[h] outside of the loop it has no problem with indexing 'h'
Any ideas on where I have made a mistake or what I could change?
If it would be better to see the full file let me know.