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.

4
  • 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? Commented Jul 19, 2021 at 17:20
  • Note that you still have another issue, in that total is defined at the wrong place... Commented Jul 19, 2021 at 17:23
  • 1
    If you want to do this concisely, just doing total = sum(range(0, 100, 2)) is enough. Commented Jul 19, 2021 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 :) Commented Jul 20, 2021 at 14:05

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

Comments

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)]))

6 Comments

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.
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
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.
I updated my answer, even if I disagree. OP wanted to print out the sum after every single addition not once.
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 :)
|
-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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.