The code given below accepts a list of numbers as an argument and returns the sum of all even numbers from the list. Observe the following code carefully and rewrite it after removing all syntax and logical errors. Underline all the corrections made.

def sum_even(lst)

    sum=0

    for i in range(length(lst)):

        if lst[i]%2=0:

            sum=sum+lst[i]

        return (sum) 

Answer:

Answer by student

def sum_even (lst) :

    sum= 0

    for i in range( len(lst) ):

        if lst[i]% 2 == 0 :

            sum=sum+lst[i]

   return (sum)

Detailed answer by teachoo

The rest of the post is locked. Join Teachoo Black to see the full post.

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo