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 trialKishore Kumar
5,451 PointsShopping 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.
5 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi 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
22,160 PointsI 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. ;)
micram1001
33,833 PointsFinished code for challenge.
def loopy(items):
for item in items:
if item[0] == "a":
continue
print(item)
Kishore Kumar
5,451 Pointswith the above code still am getting the same error. is there anything wrong using continue as I did in my code
Nathan Tallack
22,160 PointsThat 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
5,451 PointsThanks Nathan. It's working...
Kishore Kumar
5,451 PointsKishore Kumar
5,451 PointsMy Code:
error: Didn't return the right items