叶雨枫棠 2022-11-20 23:15 采纳率: 100%
浏览 3
已结题

通过while循环得到列表

问题遇到的现象和发生背景

通过while循环,在【1,2,3,4,5,6,7,8,9,10】中得到仅有偶数的列表,但我出错了

用代码块功能插入代码,请勿粘贴截图
def while_func():
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
index = 0
list2= []
while index<len(list):
    element = list[index]
    if element % 2 == 0:
        list2.append[element]
        index+=1
    print(f"通过while,源列表{list},取出偶数后的列表{list2}")
while_func()
  • 写回答

3条回答 默认 最新

  • 爱音斯坦牛 全栈领域优质创作者 2022-11-20 23:33
    关注
    def while_func():
        lis = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        list2 = []
        index = 0
        while (index < len(lis)):
            element = lis[index]
            if element % 2 == 0:
                list2.append(element)
            index += 1
        print(f"通过while,源列表{lis},取出偶数后的列表{list2}")
    
    while_func()
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月29日
  • 已采纳回答 11月21日
  • 创建了问题 11月20日