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
def loopy(items):
    # Code goes here
    items = ["abc","xyz"]
    for letter in items[0] = a:
        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

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 56,641 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'])