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 Python Basics (2015) Logic in Python Loop

Not understanding what's happening in while loop due to confusing video preceding.

Hi all, I'm a beginner at Python, and I'm stuck on understanding what the heck's happening in the code:

for (insert whatever word you want here like mtfuji) in hellos:
      print (mtfuji + "World")

I really, really don't get what the heck the value of mtfuji/dog/cactus (whatever word you want) is. How does the array know what to do with whatever value I decide, how does the array know that each part of the array, now (I guess) named mtfuji know to go to each string in the array and increment one from this, and how does the array know when to stop?

The only real world example that I can come up with that might explain what's happening is let's say we have a mancala board with one stone in each slot, and this whole mancala board is an array named: boardofstones. If we pick up each stone in the array for the time we pick it up the array now names that stone the value we assign, like prettystone. Then when we put the stone down, the array "forgets" that stone was called prettystone and then assigns that same value to the next stone that we pick up...and so forth till we reach the end of the array of stones, but again I don't know how the array knows what to do with the stones and when to stop. But I could be completely wrong in this interpretation of what's happening in the array.

Ultimately, I have no idea how the for loop in the challenge comes up with "Hello World" (and all the other Hellos in other languages + World) and knows when to stop.

*Note: I am a beginner, and I love metaphors/real world examples vs. being referred to StackOverflow, which is very tough for a beginner to think through. So please give answers in as simplistic of a fashion as possible. Thanks.

2 Answers

Hi abbymann, it seems you already have a clear understanding of for loops as your interpretation aligns with what a for loop implementation of your example would do.

You asked "how does the array know what to do...", (in this example) the print statement tells the loop what to do with every variation of hello - it simply adds/appends "World" after the current value of hello.

In terms of how the array knows its current state, how it knows to go to each string/object in the array and when to stop, all that is hidden from the developer (kindly so I'd imagine). It is done in the background so that we don't have to worry about it each time we use a for loop.

Refer to this helpful video for further explanation (simpler): https://www.youtube.com/watch?v=Q3T1yyGQd6o Or this Python documentation: https://wiki.python.org/moin/ForLoop

Hope this helps.

Evans

TEST_ID = L M_TEST
TEST_ID = L M_TEST
2,238 Points

Read this article it may help you to understand more about programming / programming languages http://www.tutorialspoint.com/computer_programming/index.htm