abhi
abhi

Reputation: 31

Find the sum of an even number from a list of integers

input-258345
    output-14
    ex-2+8+4=14
myList = input()
result = 0
for i in myList:
  if not i % 2:
    result += i

print(result)

I am getting an error:

if not i % 2:
TypeError: not all arguments converted during string formatting

Upvotes: 2

Views: 407

Answers (2)

Naaman
Naaman

Reputation: 62

mylist = list(input())
result = 0
for i in mylist:
  if int(i) % 2 ==0:
    result += int(i)
print(result)

Upvotes: 0

Mureinik
Mureinik

Reputation: 311143

myList is a string, and you are iterating over its characters. If you want to treat them as digits, you'll need to explicitly convert them. E.g.:

for i in myList:
  num = int(i)
  if not num % 2:
    result += num

Upvotes: 2

Related Questions