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 trialJeffery Austin
8,128 PointsHaving issues with challenge. Can't get this code to pass the challenge but the code does work in python shell.
def loopy(items):
if items[0] == "a":
for item in items[1:]:
print(item)
else:
for item in items:
print(item)
Not sure if this code works, but I also tried:
def loopy(items):
for item in items:
if items[0] == "a":
continue
print(item)
2 Answers
Seth Reece
32,867 PointsHi Jeffery,
Your second one is real close. Since your loop is item in items
your want to check for "a" in item, not items. e.g.
def loopy(items):
for item in items:
if item[0] == "a":
continue
print(item)
p.s. Notice that the print statement is indented to keep it inside the loop.
Evan Demaris
64,262 PointsHi Jeffery,
You original question was answered, but for your second question - why items[0] doesn't work - it's because that code would check if the first word in the list items
is "a" (instead of checking index 0 of each word), and you're just checking that first word again as many times as there are indices in the list.
Jeffery Austin
8,128 PointsAww okay I miss read the challenge thinking it was talking about the letter "a" at the index 0 of items. But if I had a list of ["a", "b", "c"]. Wouldn't this run?:
list = ["a", "b", "c"]
def loopy(items):
for item in items:
if items[0] == "a":
continue
print(item)
loopy(list)
Because I ran this in the python shell and it returns nothing, is it because the for loop is only evaluating each item and I can't check items[0] in the for loop?
Evan Demaris
64,262 PointsIt would run, but I don't think that's what you want. Assuming full code of:
def loopy(items):
# For each index of your list;
for item in items:
# If the first index of your list is "a"
# (which it will always be, for a list of ["a", "b", "c"]);
if items[0] == "a":
# At this point it is always true,
# so 'continue' through the entire list without printing.
continue
print(item)
Jeffery Austin
8,128 PointsAww so because
items[0] == "a"
is always true it continues without printing the rest of the items?
Evan Demaris
64,262 PointsRight, it never even looks at the other items to see what they are; they are only used to tell how many times to check if items[0] == "a".
Jeffery Austin
8,128 PointsOkay, thanks for that clarification. That makes a lot of sense. I should have thought of that. I had it in my head that it would work, but now that makes sense. It can't keep evaluating to True! :)
Jeffery Austin
8,128 PointsJeffery Austin
8,128 PointsThat worked! It wasn't at first because I was using:
if items[0] == "a":
instead of:
if item[0] == "a":
Although I don't understand why items[0] doesn't work as items is the list being evaluated???