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 trialCanen McCaslin
552 PointsBummer! Try again!
I have been stuck on this for 3 days but have been to embarrassed to ask for help but would greatly appreciate it. I've wrote the code in a dozen different ways but none seem to work. The instructions are as follows:
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".
def loopy(items):
if item in items == "a"[0:0]
continue
else:
print(items[0:0])
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! I see that you have thought about it and worked on it. It may be that at this point you've overthought it a bit. So let's take a walk through it:
def loopy(items):
# Code goes here
for item in items: # for each individual item in the items list
if item[0] == 'a': # if the first letter of an item is equal to a
continue # go back to the top without doing anything and get the next item
print(item) # print the item if the first letter was not "a"
So we start by setting up our for loop which is going to go through every item in our list. Our list contains a series of strings. Just like a list, we can get the character of a string by using index subscripting. The first letter will be at index 0. We ask if the item has a first letter of "a". If it does, we don't do anything else but return up to the top of the loop and get the next item. However, if the first letter was not "a" then the continue
will not be executed and it will proceed onwards to print the item before going to the next one.
Hope this helps!
David Evans
10,490 PointsHi Canen,
First you need to loop through the items. You do this with a for loop:
for item in items:
instead of
if item in items == "a"[0:0]
Inside the loop you'll do your conditional check. Checking if the first character of the current item is 'a' or not'.
if item[0] == "a":
If it is you skip it, if not you print out the current item.
This is the full code I got:
def loopy(items):
for item in items:
if item[0] == "a":
continue
else:
print(item)
Hope this helps!
Edit Looks like Jennifer beat me to it!
Canen McCaslin
552 PointsCanen McCaslin
552 PointsYou helped me understand for loops and if conditions a byte better today. Thank you!