小怪獸愛(ài)吃肉
2023-03-30 10:09:15
def skip_elements(elements) : new_list = [] for i in elements : if i % 2 == 0 : new_list= new_list.append(i) i+=1 else : i+=1 return new_listprint(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 []
1 回答

倚天杖
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
if i % 2 == 0 :
new_list= new_list.append(i)
i+=1
else :
i+=1
^ extra space - you might want to use tabs instead of spaces when it comes to python - or any language for that matter
new_list = new_list.append(i)
.append()會(huì)回來(lái)的None。將其替換為:
new_list.append(i)
最后,要返回一個(gè)包含所有奇數(shù)元素的列表需要付出很多努力。你可以簡(jiǎn)單地做(感謝@chepner):
def skip_elements(elements):
return elements[::2]
添加回答
舉報(bào)
0/150
提交
取消