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
Marie Lu
179 PointsName vs Names variable makes huge difference... why?
Here is the first code
names = ["John", "Jack", "Quit", "Jill"]
for name in names:
if name == "Quit":
continue
print(names)
I get this in console for first code, where the quit still shows up and the whole list shows up oddly three times... why does this happen
['John', 'Jack', 'Quit', 'Jill']
['John', 'Jack', 'Quit', 'Jill']
['John', 'Jack', 'Quit', 'Jill']
Here is my second code
names = ["John", "Jack", "Quit", "Jill"]
for name in names:
if name == "Quit":
continue
print(name)
I get this in console for second code
John
Jack
Jill
As you can see the only difference is when printing names I get the original list except oddly three times in a row... I thought when doing for name in names the loop would account for every name and make sure it's not quit.. is the list unchanged which is why doing names would be inaccurate? Although I don't know why it prints thrice...
If I do print(name) however, it takes out the quit yet I don't understand the logic.. I thought name is a local variable and it represents only one thing at a time.. so printing that would like only print "John"... I'm a bit confused here... maybe clear up my confusion with the for loop as well on how it functions?
2 Answers
andren
28,558 PointsThe way a for in loop works is that it takes each item in the list you give it and assigns it to the variable you provide. It then runs the code once for each item with a different item in the variable.
In other words the first time it runs your code name will be set equal to "John" (The first item) the second time it runs your code name will be set equal to "Jack" (the second item) and so on. It continues doing this until it has gone though all of the items in the list and then it stops looping the code.
That means that within the code you give the loop name will represent one of the items while names represents the list itself.
In the first code example you post you ask it to print out names during each loop, since it runs once for each item in the list it ends up printing it multiple times. The reason it is printed 3 time instead of 4 is that within the loop you ask it to check if name equals "Quit" and in that case ask it to continue. The continue keyword causes a loop to skip to the next item right away without executing the rest of the code, which means that it does not end up running the print code.
In your second code example you ask it to print out name which as explained above will contain the various names throughout the loops and that works as intended.
Ari Misha
19,323 PointsHiya Marie! Variables are like temporary containers to store some value. Almost all languages work with variables. So like whenever you wanna use the value later , thats stored in a variable, all ya have to do is reference the variable in your code, and computer will know the value stored in the Variable container.
But computers aren't smart enough to read whats going on in human heads. So yeah "Name", or "name" or "Names" or "names" are four different storage containers, right?
As far as your code goes, there is a subtle error in it. In loops, the order of statements matter. Like first statement gets executed first , followed by second statement and so on. Now lets put some highlights on your code. In your "for" loop, Python encounters an "if" conditional so its checking the condition but on what? There is no "Quit" to check in "name"(s) . Catch my drift? So it ignores the whole "if" conditional coz there is nothing to apply conditional to. Hence it loops three times without skipping over "QUIT".
Now try it this way and review the code:
names = ["John", "Jack", "Quit", "Jill"]
for name in names:
print(names)
if name == "Quit":
continue