0

Hello (beginner here),

I am trying to write a script that prints the sum of every even numbers in the range [0; 100].

However, I am getting a "TypeError: 'int' object is not iterable".

This is my code:

for i in range(0, 101):
    total = 0
    if i % 2 == 0:
        total = sum(i)
        print(total)

My error message:

Traceback (most recent call last): File "", line 4, in TypeError: 'int' object is not iterable

I've searched this website and Google to understand what I'm doing wrong, but I cant seem to grasp an answer to my specific code.

Any help would be greatly appreciated.

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • 1
    ``sum`` computes the sum *of its argument*, e.g. ``sum([1, 2, 3]) == 6``. Passing it a single integer is not sensible. Did you mean ``total += i`` instead? – MisterMiyagi Jul 19 '21 at 17:20
  • Note that you still have another issue, in that ``total`` is defined at the wrong place... – MisterMiyagi Jul 19 '21 at 17:23
  • 1
    If you want to do this concisely, just doing ``total = sum(range(0, 100, 2))`` is enough. – MisterMiyagi Jul 19 '21 at 17:24
  • Hello @MisterMiyagi (awesome name) YES !!!! You are correct on all those comments. It was a good learning experience for me. Thank you :) – AndrewGLeong Jul 20 '21 at 14:05

3 Answers3

2

The problem is with the sum function it is not used in that manner. If you give 1 argument then it should be a list then you can use .append() function but in your use-case you can simply use + i.e., addition.

total = 0
for i in range(0, 101):
    if i % 2 == 0:
        total += i
        print(total)
Uttam
  • 718
  • 6
  • 19
0

The sum() function can only sum up an iterable (list, dict, etc.). In your loop, your variable i is seen as a solid number, not a list. If you want to sum up every even number, you need to add i to a list:

list1 = []
for i in range(0, 101):
    if i % 2 == 0:
         list1.append(i)
         total = sum(list1)
         print(total)

Here is a better version, if you do not want the output after every single addition:

print(sum([i for i in range(0,101, 2)]))
Dakopen
  • 62
  • 11
  • 1
    This computes the sum of all *prefixes* of the list of even integers, and expensively as well. You compute `0`, then `0+2`, then `0 + 2 + 4`, etc, rather than just keeping a running total. – chepner Jul 19 '21 at 17:30
  • In general, I agree with you. In practice is the execution speed not noticeable for his/her use case. I thought he/she wants to use the sum() function and this is the easiest way to show how it works – Dakopen Jul 19 '21 at 17:33
  • The question is pretty clear: the OP wants the sum of the even integers between 0 and 100. `sum` lets you do that *without* an explicit loop. There's no reason to suggest that code like this is acceptable. – chepner Jul 19 '21 at 17:35
  • I updated my answer, even if I disagree. OP wanted to print out the sum after every single addition not once. – Dakopen Jul 19 '21 at 17:40
  • Hi Dakopen - this is what I wanted and you showed me a way to do it with printouts of a running total and a grand total. Thank you for helping me. I'm new to Python, programming and StackOverflow :) – AndrewGLeong Jul 19 '21 at 20:45
  • I found that if you take the print statement out of the function, it just prints the total too which is also awesome. – AndrewGLeong Jul 19 '21 at 20:58
-3

the answer to your question using sum function can simply be

for i in range(0, 101, 2):
    total = 0
    total = sum(i, total)
    print(total)

Learn more about Python loops here!
Learn Python fundamentals here!

Haripriya
  • 17
  • 3