0
Thats odd... (Problem)
https://www.sololearn.com/coach/63?ref=app How to solve this? I used the following code.. But it does not work y = [] while True: x = int(input()) if x%2 == 0: y.append(x) print(sum(y))
6 Answers
+ 3
In ur code u have to set an upper limit .. u have to take an input form the user which is the length of that list.. and using loop u have to take inputs those N values..
Here's my try .
Hope it helps..
N=int(input())
l1=[]
for i in range(N):
N=int(input())
l1.append(N)
ct=0
for i in l1:
if(i%2==0):
ct+=i
print(ct)
+ 3
Billy Beagle
Q&A is reserved for coding related questions only.
If you wish to spam the community, please do so from your personal feed.
You may wish to read the Sololearn guidelines before your account gets suspended or cancelled.
If you can't find the guidelines, let us know and we will post them for you.
I suggest you delete this post
+ 3
Ramprasad Saha
Using a while loop without an exit will cause the sum value to increase infinitely.
The loop never stops and you never print.
Set up your upper limit
Also, your loop only looks at the input number, not a range of numbers.
+ 3
Ramprasad Saha
I just reviewed the challenge & you are missing an entire section.
Input Format:
The first input denotes the length of the list (N). The next N lines contain the list elements as integers.
So you must create an int(input()) which will then be used to generate that amount of numbers.
nums = []
for i in range(int(input()):
nums.append(int(input())
That creates a range of the N, which then allows more numbers to be generated & added to the empty list called nums.
Now we have to determine which of those numbers are even, and add them together
Output Format:
An integer that represents the sum of only the even numbers in the list.
result = 0
for i in nums:
if i%2 ==0:
result +=i
print(result)
That section of code iterates through the nums list looking for even numbers.
If the number is even, it adds the number to the result variable
Which should print the result
+ 1
I actually did not understand that u have to set an limit for the inputs..
Now i understand..
Indira and Rik Wittkopp thank you both so much for your help.
0
Rik Wittkopp i tried this one as well..
But doesnt work
x = range(int(input())+1)
z = list(x)
p =[]
for i in z:
if i%2 == 0:
p.append(i)
print(sum(p))