Writing a function that alternates plus and minus signs between list indices

后端 未结 7 803
既然无缘
既然无缘 2020-12-10 07:40

In a homework set I\'m working on, I\'ve come across the following question, which I am having trouble answering in a Python-3 function:

\"Write a fun

7条回答
  •  攒了一身酷
    2020-12-10 08:19

    my_list = range(3, 20, 2)
    sum(item * ((-1)**index) for index, item in enumerate(my_list))
    

    sum = 11 (result of 3-5+7-9+11-13+15-17+19)

提交回复
热议问题