【发布时间】:2023-03-11 04:13:01
【问题描述】:
编辑:将“hi”更新为“list”
我是一名初学者,我一直致力于将英语句子翻译成猪拉丁语的项目,但在尝试删除句子中标点符号前的空格时遇到了问题。这是我遇到问题的脚本。
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)
当我运行它时,我得到了错误:
File " ... ", line 110, in <module>
if list[h] in string.whitespace:
IndexError: list index out of range
当我在循环外打印 Hi[h] 时,索引 'h'
没有问题关于我在哪里犯了错误或我可以改变什么的任何想法?
如果看到完整的文件更好,请告诉我。
【问题讨论】:
标签: python variables index-error