【发布时间】:2020-09-15 10:11:46
【问题描述】:
def skip_elements(elements):
new_list = []
i = 0
for a in elements:
print(a)
if i%2==0:
new_list+=a
i+=1
return new_list
print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach']
print(skip_elements([])) # Should be []
这是显示缩进错误。当我们将 print(a) 放在 i+=1 之前时,即使这样也不起作用,而下面的代码却完美无缺。为什么会这样??
def skip_elements(elements):
new_list = []
i = 0
for a in elements:
if i%2==0:
new_list+=a
i+=1
print(a)
return new_list
print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g']
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach']
print(skip_elements([])) # Should be []
【问题讨论】:
-
您收到的确切错误消息是什么?
-
文件“rough.py”,第 6 行 print(a) ^ TabError: 缩进中制表符和空格的使用不一致
-
似乎是您的编辑器问题,而不是 python。检查这个问题:stackoverflow.com/questions/5685406/…
标签: python python-3.x list for-loop printing