mardi 1 août 2017

Write a function that calculates the sum of all values in a list of integers that are both positive and even

The function should accept a single list as a parameter. The function should return an integer value as the result of calculation. If there are no positive and even integer values in the list, your function should return 0.

My current code:

def main():
print (sum_positive_even([1,2,3,4,5]))
print (sum_positive_even([-1,-2,-3,-4,-5]))
print (sum_positive_even([1,3,5,7,9]))

def sum_positive_even(list):
for num in list:
    if num < 0:
        list.remove(num)
for num in list:
    if num % 2 == 1:
        list.remove(num)
result = sum(list)
return result

main()

The output should be like:

6
0
0

I'm confused where I should put the 'return 0'. Thanks TA!

Aucun commentaire:

Enregistrer un commentaire