+ 1
summartion of even nums
Task: Find the sum of all even integers in a list of numbers. Input Format: The first input denotes the length of the list (N). The next N lines contain the list elements as integers. Output Format: An integer that represents the sum of only the even numbers in the list. Sample Input: 9 1 2 3 5 I have something like this, it works as they said, but I couldn't pass the challenge. def sum_even(numbers): sum_nums = 0 for number in numbers: if number % 2 == 0: sum_nums += number elif number % 2 == 1: sum_nums += 0 return sum_nums nums = [1,2,3,4,5,6,7,8] print(sum_even(nums))
2 Respostas
+ 1
Have you created the list first?
Also, elif is redundant code, even else can be omitted.
+ 1
def sum_even(numbers):
sum_nums = 0
for number in numbers:
if number % 2 == 0: sum_nums += number
return sum_nums
nums = []
n = int(input())
for i in range(n):
nums.append( int(input()) )
print(sum_even(nums))