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 (Retired) Shopping List Shopping List Project

Chenxing Liu
Chenxing Liu
691 Points

what does the 'item' do near the end of the code

Hi everyone, how are you.. a noob here. it's been clear and easy to follow this course but i got a bit confused in this chapter near the end of the code, plz help me. Originally the code is demonstrated like this:

shopping_list = list()

print("what are you shopping?")

print("Enter DONE when done.")

while True: new_item = input ("> ")

if new_item == 'DONE': break

shopping_list.append(new_item) print ("added! listing has now {} new items".format(len(shopping_list))) continue

print("Here it is:") for item in shopping_list: print(item)

My question is 'item' has never been defined in the codes above, there was only 'new_item', how did python recognize what i mean. Also I don't know what that new_item = input ("> "), what ">" does? So i decided to test with sth else by messing with the names (for reference here is the entire code):

shopping_list = list()

print("what are you shopping?")

print("Enter DONE when doned.")

while True: new_cat = input ("> ")

if new_cat == 'DONE': break

shopping_list.append(new_cat) print ("added! listing has now {} new items".format(len(shopping_list))) continue

print("Here it is:") for wtf in shopping_list: print(wtf)

It works! I wonder why now... Thanks for the answers in advance and happy holidy.

1 Answer

Mikael Enarsson
Mikael Enarsson
7,056 Points

Firstly, please look at the Markdown Cheatsheet for how to post code. It really makes it much easier to read your code and understand your question!

Secondly, item HAS been declared (or defined, under certain definitions), and it gets assigned a value every time the for loop passes. I'll try to give an example to explain it:

a_list = [3,5,7,9]        #Just a random list. In this case it's pretty arbitrary, just for demonstration purposes
sum = 0                   #I'll use the for loop to add the values in a_list. sum will hold the sum value

for item in a_list:       #Here's where the magic happens. This code means that, for every item, that is value at a specific
                          #index in a_list, perform the operation in the for loop with item taking the value of the list item
                          #at that specific index position.
  sum += item

print(sum)                #In this example, sum will hold 24 as its value after the loop finishes, which is the sum of the
                          #items in the list

I hope that that clarified it to you somewhat. If you still have questions, please don't hesitate to ask!

Chenxing Liu
Chenxing Liu
691 Points

Thank you so much Mikael for the answer and patience. Sorry i didn't know about the cheatsheet, i will follow the guide strictly from nowon.

Ok if i understand correctly item is a predefined term? I am confused because it looks like item doesn't have to be called 'item'... it can be called whatever, and even if whatever has not been defined or declared before. e.g.

for item in a_list:
   sum += item

#will do the same thing as :

python
for whatever in a_list:
   sum +=whatever

Thanks again.

Mikael Enarsson
Mikael Enarsson
7,056 Points

It's fine, we have all been there (well, at least I have!) ^^

As to your question, no, it isn't a predefined term. You can actually call it whatever you want! What I meant was, item was declared in the loop header (or, once again, whatever is the correct term XD), and it gets assigned a new value for each pass through the loop. For example

a_list = [1, 2, 3]
sum = 0

for item in a_list:
  sum += item

is the same as

a_list = [1, 2, 3]
sum = 0

item = a_list[0]    #  First pass through the loop
sum += item

item = a_list[1]    #  Second pass through the loop
sum += item

item = a_list[2]    #  Third pass through the loop
sum += item

In the first example, as in the second you can give any name to the variable (a_number, sausage_roll, mr_pretty, whatever you want!) ^^

I hope that was clearer!

Chenxing Liu
Chenxing Liu
691 Points

Ah now i understand, it is justified the moment it is placed into that particular spot, wow it is so efficient and actually intuitive. Thank you Mikael, happy new year.

Mikael Enarsson
Mikael Enarsson
7,056 Points

It's indeed! And it's good to know, since the same kind of thing exists in most if not all programming languages!

You're welcome, and a happy new year to you too!