+ 2
You want to take a list of numbers and find the sum of all of the even numbers in the list. Ignore any odd numbers.
Please help me to solve this with python.
20 ответов
+ 1
num = [ 1, 2, 3, 4]
for x in num:
if x % 2 == 0:
print(x+0)
0
Your solution?
0
Use for loop:
Here is my solution:
List = [ list of numbers]
sums = 0
for i in List:
if i % 2 == 0 :
sums += i
print(sums)
0
It's not working.. invalid syntax.expected output is 278
0
Kalyani Khakal
I guess the problem is with Print function. Make sure that all the words in print() are lowercase. I forgot to fix it when i was writing.
0
Ok
0
It still not working,I think the problem is in 1st line
0
Can i see your code?
0
List = [1,2,3,4,5,6,7,8,9]
sums = 0
for i in List:
if i % 2 == 0 :
sums += i
print(sums)
0
This code is working fine. I don’t know what is the problem.
Here you can test it yourself:
https://code.sololearn.com/cw7poVLUtep3/?ref=app
0
Thank u for your help..
0
No problem mate. :D
0
Java
0
You can use another list for storing even numbers within a loop the put that new list in the sum() function
0
Community organization and would
0
Use this code:
l= []
for i in list_numbers:
if i%2==0:
l.append(i)
else:
pass
print(sum(l))
0
Please help me to go further in my lessons in solo my heart finished and i can do anything just discussing
0
Better use numpy and sort and filter them then just simple addition will suffice
0
Why to suffer, try this:
a = list(range(26))
print(sum(filter(lambda x: x%2==0,a)))
0
Code:
1. nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2. sum = 0
3. for num in nums:
4. if(num%2==0):
5. sum = sum + num
6. print(sum)
Explanation:
1. Initializes a list of numbers from 1 to 10
2. Initializes the value of sum to 0
3 & 4. Checks whether any value in the list is even
5. Adds these values together and stores them in the sum variable
6. Finally prints the sum