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

Viewed 383
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
2 Answers

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
mylist = list(input())
result = 0
for i in mylist:
  if int(i) % 2 ==0:
    result += int(i)
print(result)
Related