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 trialSebastian Knell
927 PointsI don't understand this exercise. I did what I could. Help please!!
Maybe the indentation is wrong, I don't know
def loopy(items):
for item in items:
if item.index(0) == "a":
continue
else:
print(item)
2 Answers
andren
28,558 PointsYour code is actually very close to correct. The issue is that the index
method does not function in the way you might expect, it is actually used to find the index of a character, not to get something from an index.
Meaning that it expects to be passed something like a letter and will then tell you what index that letter is at.
So to check if a
was at index 0 using the index
method you would have to reverse the current logic of your code like this:
item.index("a") == 0
However the index
method is not really well suited for this task to begin with, because it will throw an exception (basically an error) if it does not find the character you specify.
The ideal way to check the first letter in this task is to use bracket notation. With bracket notation you can just type the name of a string or list followed by a pair of brackets with an index. Like this:
item[0]
And that will return the letter at that index.
If you use that in your code like this:
def loopy(items):
for item in items:
if item[0] == "a":
continue
else:
print(item)
Then your code will work.
mhjp
20,372 PointsTry indexing your string like so.
item[0]
Sebastian Knell
927 PointsThanks
Sebastian Knell
927 PointsSebastian Knell
927 PointsThanks