+ 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.
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 !
+ 10
Thanks 🇮🇳 AYUSH.ks 🇮🇳 and Manu_1-9-8-5 .
+ 8
🇮🇳 AYUSH.ks 🇮🇳 can we use continue instead of pass.
+ 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
+ 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
+ 3
Hay Intenzi its edited
+ 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)
+ 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.
+ 3
🇮🇳 AYUSH.ks 🇮🇳 ty its perfect, to confirm the pass is used when u dont want anything in the else parameter?
+ 3
Yes Intenzi you got it
+ 2
What exactly is not like you expected it?
I entered 3 for N, then 2, 5, 6 and got:
2
0
8
+ 2
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 I dont understand exactly how that code worked in a single line but thats awesome!
+ 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
+ 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)
+ 2
Puthur Harthik or you just remove the else-block as it is useless like this when we have no follow up code.
+ 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)
+ 1
I wish for it to output only the final sum...
+ 1
you need to remove the else statement and the print(sum) must be after for statement
+ 1
By the way, you can do it all in one loop and you don’t need the list