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) Shopping List App Continue

Shopping list Task

Same idea as the last one. My loopy function needs to skip an item this time, though.

Loop through each item in items again. If the character at index 0 of the current item is the letter "a", continue to the next one. Otherwise, print out the current member.

Example: ["abc", "xyz"] will just print "xyz".

I have written the following code but gives me wrong answer, pls help me in understanding this

breaks.py
def loopy(items):
    # Code goes here
    items = ["abc","xyz"]
    for letter in items[0] = a:
        continue
    else:
        print "items[0]

1 Answer

Hey CH! Couple of things here. The variable items is is already defined, and will be sent to the function when you "check answer" therefor you do not need to define it yourself. Secondly you have some issues in your for loop. To outline, we simply want to loop thru every item in the list "items" and check if the current item has an "a" at index 0. Ill provide the correct code to demonstrate this:

def loopy(items): #Here the items list is already defined.
    for i in items: #This will loop thru every item in the list "items" and assign current item to the variable "i"
        if i[0] == "a": #Checking if the index 0 of the current item is the same as the string a
            continue #If this is the case, just coninue to the next item, but dont do anything.
        else: #If this is not the case however, print the item
            print(i)

Thank you Behar, I tried in shell by defining variables in items & when i called the function it worked fine. wondered why i was not able to clear the task. Thanks for helping.

Tried out in shell:

>>> def loop(items):
...     items =['las','a','m','x','d','t']
...     for letter in items:
...         if letter == 'a':
...             continue
...         else:
...             print(letter)
... 
>>> loop(items)

[MOD: added ```python formatting -cf]

Glad to help! You Can mark the question as "solved" by sellecting a "Best answer".

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Do not assign items. This variable needs to be set by the challenge checker. To test your code, call it using items(['las', 'a', 'm', 'x', 'd', 't'])