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 trialMarcelo Alves
16,547 PointsHow do I return list value and index to use as conditions?
I'm trying to avoid printing the first iten of a list if its idex is 0 and if it's the letter 'a'.
However, I don't know why my code bellow doesn't work.
def loopy(items):
for iten in items:
if items.index(iten) == 0 and iten == 'a':
continue
else:
print(iten)
Anyone could help me?
def loopy(items):
# Code goes here
3 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHey Marcelo,
(I added markdown to your post, so the code is more readable. You can see how in the Markdown Cheatsheet.)
There are a few things going on here. First, there is a spelling error in your code. You have iten
instead of item
and this matters in code challenges.
Second, you aren't checking for a index, the challenge wants you to check the value at a specific index. In this case, it wants you to see if the value at index 0
is an "a" or not. So it's just a single if
statement to check one thing.
Other than that, you are on the right track.
Below is the corrected code for your reference. I hope it all makes sense.
def loopy(items):
# Code goes here
for item in items:
if item[0] == "a":
continue
else:
print(item)
Keep Coding! :)
John Simmons
1,247 PointsWhile you are at it, check out enumerate if you are evaluating items and their indexes
Marcelo Alves
16,547 PointsFellows,
Thanks for the help.
I hope one day be abble to help other people like you guys.
Regards