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 trialCH. K Sri Krishna
516 PointsShopping 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:
continue
else:
print "items[0]
1 Answer
behar
10,799 PointsHey 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)
CH. K Sri Krishna
516 PointsCH. K Sri Krishna
516 PointsThank 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:
[MOD: added ```python formatting -cf]
behar
10,799 Pointsbehar
10,799 PointsGlad to help! You Can mark the question as "solved" by sellecting a "Best answer".
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsDo not assign
items
. This variable needs to be set by the challenge checker. To test your code, call it usingitems(['las', 'a', 'm', 'x', 'd', 't'])