0d: 00h: 00m: 00s

🎁 Get 3 extra months of learning — completely FREE

That's 90 extra days to master new skills, build more projects, and land your dream job.

Become a PRO
Background Image

🎁 Get 3 extra months of learning completely FREE

Become a PRO
ymlsurgeon
PRO
last week
Ymlsurgeoncountry asked

My loop syntax looks right, but range() isn’t reaching the final number. What am I missing?

Abhay Jajodia
Expert
last week
Abhay Jajodia answered

Great question. This trips a lot of people up when they first start using Python.

The key thing to remember is that range() stops one number before the value you put in as the second argument. So if you write:

range(1, n)

Python will count like this:

1, 2, 3, ..., n-1

It never reaches n, which is why your loop feels like it’s stopping too early.

If you actually want to include n in the loop, just add 1 to the stop value:

for i in range(1, n + 1):
    total_sum += i

Now Python will count:

1, 2, 3, ..., n

and you get the full sum you expected.

If anything still feels confusing, I’m happy to walk through another example with you.

Python
This question was asked as part of the Practice: Python Basics course.