+ 1

Python program to find sum of all even numbers in a list of numbers. Can’t find what I did wrong ;-;

#The program asks to input first the number of values u will .be adding and then the numbers that ur inputting N = int(input()) List = [] sum = 0 while N > 0: List.append(int(input())) N -= 1 for num in List: if num % 2 == 0: sum = num + sum print (sum) else: print (0) This should work, but it isn’t working idk why. I’ve been stuck at finding what went wrong for a day can anyone help me.

10th May 2020, 11:27 AM
Intenzi
Intenzi - avatar
39 odpowiedzi
+ 5
Try this N = int(input()) List = [] list = [] while N > 0: List.append(int(input())) N -= 1 for num in List: if num % 2 == 0: list.append(num) else: pass print(sum(list)) Fixed !
10th May 2020, 11:37 AM
Ayush Kumar
Ayush Kumar - avatar
12th May 2020, 1:12 PM
Puthur Harthik
+ 8
🇮🇳 AYUSH.ks 🇮🇳 can we use continue instead of pass.
12th May 2020, 6:16 AM
Puthur Harthik
+ 7
Yes Rithea Sreng you are true . A single task can be written in various way . But the main thing is that to reply the answer in the format in which a asker is asking
10th May 2020, 12:13 PM
Ayush Kumar
Ayush Kumar - avatar
+ 5
AYUSH.ks, if you use sum ( as identifier) and sum() function in a code it will bring up a TypeError. The reason of this is, that by using sum as an identifier name you overwrite parts of the sum() object. You can check this with this code: ( run either first or second part of the code separately) my_sum = 4 +3 lst = [1,2,3] total = sum(lst) # NO error sum = 4 +3 lst = [1,2,3] total = sum(lst) # TypeError: 'int' object is not callable
10th May 2020, 1:48 PM
Lothar
Lothar - avatar
+ 3
Hay Intenzi its edited
10th May 2020, 11:57 AM
Ayush Kumar
Ayush Kumar - avatar
+ 3
Or try this N = int(input()) List = [] sum = 0 while N > 0: List.append(int(input())) N -= 1 for num in List: if num % 2 == 0: sum = sum + num else: pass print(sum)
10th May 2020, 12:00 PM
Ayush Kumar
Ayush Kumar - avatar
+ 3
I only wanted to give a very general remark: Do not use identifier names (e.g. for variables) that are identical with python keywords or buil-in functions. So "sum" should not been used, becuase this name is used for a function. If you do it anyway, it can have unexpected consequences. You can use like "sum_" instead.
10th May 2020, 12:02 PM
Lothar
Lothar - avatar
+ 3
🇮🇳 AYUSH.ks 🇮🇳 ty its perfect, to confirm the pass is used when u dont want anything in the else parameter?
10th May 2020, 12:04 PM
Intenzi
Intenzi - avatar
+ 3
Yes Lothar aggred but if we force python to so then it will obviously do . Because sum() and sum are not same . one is a variable and another is function.
10th May 2020, 12:05 PM
Ayush Kumar
Ayush Kumar - avatar
+ 3
Yes Intenzi you got it
10th May 2020, 12:06 PM
Ayush Kumar
Ayush Kumar - avatar
+ 2
What exactly is not like you expected it? I entered 3 for N, then 2, 5, 6 and got: 2 0 8
10th May 2020, 11:34 AM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 2
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 I dont understand exactly how that code worked in a single line but thats awesome!
10th May 2020, 12:00 PM
Intenzi
Intenzi - avatar
+ 2
n=int(input()) a=[] for j in range(n): m=int(input()) a.append(m) sum=0 for i in range(n): if (a[i])%2==0: sum=sum+a[i] else: sum=sum+0 print(sum) Try this this will work correctly
11th May 2020, 10:00 AM
BHAVESH UTTAWANI
BHAVESH UTTAWANI - avatar
+ 2
N = int(input()) List = [] sum = 0 while N > 0: List.append(int(input())) N -= 1 for num in List: if num % 2 == 0: sum = num + sum #so don't print the sum at a time else: #to ignore odd numbers continue #print the sum out of loop print(sum)
11th May 2020, 8:58 PM
Hamza Bouguendou
Hamza Bouguendou - avatar
+ 2
Puthur Harthik or you just remove the else-block as it is useless like this when we have no follow up code.
12th May 2020, 10:42 AM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 2
This should give you what you are looking for: list = [1,2,3,4,5,6,7,8,11,10] sum = 0 for i in list: if i%2 ==0: sum += i print (sum)
12th May 2020, 11:36 AM
Nahum Ndandok
Nahum Ndandok - avatar
+ 1
I wish for it to output only the final sum...
10th May 2020, 11:36 AM
Intenzi
Intenzi - avatar
+ 1
you need to remove the else statement and the print(sum) must be after for statement
10th May 2020, 11:36 AM
Gabriel Ilie
Gabriel Ilie - avatar
+ 1
By the way, you can do it all in one loop and you don’t need the list
10th May 2020, 11:37 AM
Gabriel Ilie
Gabriel Ilie - avatar