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

Kishore Kumar
Kishore Kumar
5,451 Points

Shopping list

Same idea as the last one. My loopy function needs to skip an item this time, though. Loop through every item in items. If the current item's index 0 is the letter "a", continue to the next one. Otherwise, print out every member of items.

Kishore Kumar
Kishore Kumar
5,451 Points

My Code:

def loopy(items):
    # Code goes here
    items = []
    for i in items:
      if items[0] == 'a':
        continue
      else:
        print(i)

error: Didn't return the right items

5 Answers

Hi Kishore,

There was nothing wrong with the continue statement and I think that's what the challenge instructions were leading you to do. Nathan's alternate solution is fine to.

You did have 2 issues with your original code though.

First, you assigned an empty list to items with items = [] This means you've lost whatever items were passed into your function by the code challenge tester. So now your for loop doesn't execute even one time because there's nothing in items.

Second, in your for loop you are storing each item in the variable i as you loop over items. So you want to check index 0 of the current item which is i in your case not index 0 of the items list that was passed in.

Use if i[0] == 'a': instead.

Nathan Tallack
Nathan Tallack
22,159 Points

I imagine there are quite a few ways to do this, but here is what I did.

I figured "Well, I want to focus on the printing." So here is what it looks like.

def loopy(items):          # Start my loop over the list
    for item in items:     # Grab each item out of the list
      if item[0] != "a":   # If the item does NOT start with the letter a
        print(item)        # Print it out

No need for the continue then because if it will loop back all by itself. In this way the items starting with the letter a will not be printed, and everything else will be. ;)

Finished code for challenge.

def loopy(items):          
    for item in items:     
      if item[0] == "a":
        continue
      print(item)   
Kishore Kumar
Kishore Kumar
5,451 Points

with the above code still am getting the same error. is there anything wrong using continue as I did in my code

Nathan Tallack
Nathan Tallack
22,159 Points

That is very strange. I just went and double checked the challenge and it is passing just fine for me.

Can you confirm that you are putting it in the challenge you have linked up in the top right of this post under the "View Challenge" button?

Kishore Kumar
Kishore Kumar
5,451 Points

Thanks Nathan. It's working...