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 trialLaurence Peberdy
Courses Plus Student 467 PointsWhy "spot =+ 1"?
Hi, two questions about the usage of "spot =+ 1":
Firstly, if the purpose of the "spot" variable is to capture a new location within which the user wants to index an item every time it's looped through, why do we want to add a 1 to the variable every time?
Secondly, the way I interpret this code, every time the script loops through, it should reset the value of spot so that it equals the number entered as an index:
"spot = int(index) - 1"
e.g. First loop: spot = "given index location" - 1 to provide "desired index location". spot is referenced to place the item in the list 1 is added to spot
e.g. Second loop: spot = "given index location" - 1 to provide "desired index location" etc..
Why is the value written at the end of the first loop not overwritten when spot is given a new value in the second loop?
Apologies if this is a stupid question, I'm just struggling to get my head around it.
Laurence Peberdy
Courses Plus Student 467 PointsHi, thanks for the answer.
With regards to the second question, apologies if it was unclear. My perspective is that of a beginner, which is probably hard to make sense of if you have more experience.
For some reason I did not realise that the for loop placed all items from new_list in to shopping_list (I thought it only placed items if an index value was given and then other items were placed in to shopping_list in another loop).
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsGiven the following code:
spot = int(index) - 1
for item in new_list:
shopping_list.insert(spot, item.strip())
spot += 1
spot
is set to the the integer value of index
minus 1. Because users like to interact in a list indexes starting at 1, 2, 3... and the spot
will hold the "minus 1" value to interact with the shopping_list
indexes starting at 0, 1, 2....
For the first iteration of the loop spot
equals the user's desired insert location. The .insert()
method puts the first item
from new_list
at that index and all following shopping list elements are shifted over. Before the second loop starts spot
is incremented by 1 so that the insertion point is move along to follow the item just inserted.
For the second loop, the next item from new_list
is assigned to item
and the loop begins. The insertion takes place at the location following the previous insertion. spot
is incremented again in preparation for the third loop.
If spot
was not incremented, all insertions would happen at "index minus 1", which would effectively reverse the order the user listed in the cases where more than one item in new_list
.
spot
does not get reinitialized to "index - 1" because that statement is previous to and not in the loop.
Laurence Peberdy
Courses Plus Student 467 PointsHi, thanks very much, I understand now.
For some reason I did not realise that the for loop placed all items from new_list in to shopping_list (I thought it only placed items if an index value was given and then other items were placed in to shopping_list in another loop).
Russell Holz
4,811 PointsRussell Holz
4,811 PointsOk so first off, the syntax is spot += 1, which means that each time through the loop the amount you will be adding to spot is 1. The spot variable has to change otherwise, you would have an index error when you tried to add two things to a single index in the list. I'm not quite sure what you were driving at with the second question, but I'll take a stab the value is not overridden because the "spot" variable is increasing changing the index at which you are adding an item to the list. If you didn't increase spot by 1 each time you looped through then you would be continually overriding the value at position 1 (index 0). Hope that helps.