Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Functions, Packing, and Unpacking Getting Info In and Out of Functions Function Gotcha's

Gard Sveipe Bahmanyar
Gard Sveipe Bahmanyar
1,693 Points

Could someone explain why its 2,3,4,5,6 and not 2,4,6,8 when it is +2 in the function?

Could someone explain?

3 Answers

Blaize Pennington
Blaize Pennington
13,879 Points
for num in range(10):
    print(add_two(num))

This code is literally saying for every number in the range of 10 (range starts at 0 and ends before the number so 9.)

So this will create a loop that enters each of these.

print(add_two(0)) // 0 + 2 = 2
print(add_two(1)) // 1 + 2 = 3
print(add_two(2) // 2 + 2 = 4
print(add_two(3)) // 3 + 2 = 5
etc
Jeffery Jones
Jeffery Jones
2,769 Points

This was very helpful since range has not been explained in the videos yet. Thanks!!

Leah Kelly
Leah Kelly
4,141 Points

another thing: 10 + 2 isn't on there because range() does everything BEFORE the number inside it.

I totally agree with Ashley.. I have no idea wtf the purpose of this function would be. I mean I understand what it does.. but it seems useless for actual programming. Hopefully it will make more sense the more I learn, but I can think of no situation where this particular function has a place.