我正在尝试编写一个函数来提取任何给定单词的所有其他字母。有人可以解释一下为什么这段代码不产生所有其他字母吗?
def other_letter(word):
other_letter = ""
for letter in word:
if word.index(letter) % 2 == 0:
other_letter += letter
return other_letter
print(other_letter("Hello World!"))
Run Code Online (Sandbox Code Playgroud)
我认为如果索引的模等于 0 那么它应该拉出所有其他字母并将其添加到other_letter? 我找到了另一种方法来解决这个问题,无需模运算符,但我想知道为什么模运算符在这里不起作用?