+ 2

What's wrong with my code?

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. 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 4 5 6 7 8 9 Sample Output: 20 length_of_the_list = int(input()) list_elements = int(input()) list_of =list (range(0,length_of_the_list) sum_of_even = 0 for number in list_of : if number % 2 == 0 : sum_of_even = sum_of_even + number print(sum_of_even ) else: print(0)

10th Nov 2021, 11:48 PM
siviwe sam-sam
10 Answers
+ 1
siviwe sam-sam Python cares about indentation. Not as Java,html which uses indentation for readability purposes and coding etiquettes. As you might have seen from the beginning of the course of Python, they clearly mentioned to pay attention when indenting in python. Hope this help.
12th Nov 2021, 9:44 PM
Danny-Durand NZIGAMASABO
Danny-Durand NZIGAMASABO - avatar
+ 2
you take the length of the list, but dont do what you're supposed to with it. Read the input instructions again. Try using a for loop as you append each input digit to a list. Then print the sum of the evens from that list
11th Nov 2021, 12:26 AM
Slick
Slick - avatar
+ 2
siviwe sam-sam Your loop should loop N times. N is an integer and is not iterable. Try using the range() function to loop N times like: for _ in range(N): You can use an underscore _ to signify that you're not using the current resulting value of the iteration, this is the Pythonic way to do it. Also, n = N You should be getting the next input() here as an integer. Much like you did for N above. Then after the loop output the value for total.
11th Nov 2021, 12:58 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
You need to loop N times to get the input() of the numbers. Each number is given on a new line as an individual input, not altogether as a list. Inside the loop, you can get the input of the number, check if it is even, if even add it to the total (don't use the variable name 'sum' as it will shadow the name of the built-in function sum() and is a bad practice). After the loop, output the total of the even numbers. pseudocode: N = input received as int total = 0 loop N times: n = input received as int if n is even: total += n output total
11th Nov 2021, 12:26 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
siviwe sam-sam Remove indentation of last print statement. In your code it is under for loop scope
12th Nov 2021, 6:12 PM
Ilyas Bakirov
Ilyas Bakirov - avatar
0
N = int(input()) total = 0 for n in N : n = N if n % 2 == 0 : total = total + n what's wrong now?
11th Nov 2021, 12:50 AM
siviwe sam-sam
0
N is only a single number. It's supposed to represent the literal number of numbers that will be input after. So you can't use a for loop in a single number. YOU CAN use a for loop with a range of numbers though: for n in range(N): ... You could just make a seperate variable within the loop and use it to gather input, then use the logic you used to add it to the total if the number is even.
11th Nov 2021, 12:56 AM
Slick
Slick - avatar
0
N = int(input()) total = 0 for _ in range(N) : n = int(input()) if n % 2 == 0 : total = total + n print (total) here my code the out put gives me the correct answer but it is showing it many times how to make it out put it one time
11th Nov 2021, 2:12 PM
siviwe sam-sam
0
the print statment should be at the same indent level as the for loop. That would take the print statement out of the loop making it only print once
11th Nov 2021, 3:37 PM
Slick
Slick - avatar
- 1
ada yang bisa hack wa menggunakan python
12th Nov 2021, 10:58 PM
Syahrul Ramadhan