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 trial

Python

coding challenge index

I am having trouble with this coding challenge https://teamtreehouse.com/library/python-basics/shopping-list-app/continue this is my code; items=["a","b"] def loopy(items): for item in items: if item.index("a")==0: continue print(item)

loopy(items)

It tells me that substring "a" can not be found.

I also tried to catch this error like this: def loopy(items): for item in items: try: if item.index("a")==0: except ValueError
continue print(item)

wich gives me a syntax error. Could somebody tell me whats wrong with this code?

1 Answer

You mixed it up a little :)

You said:

item.index("a") == 0

And I think that should work but I think the challenge is expecting

item[0] == "a"

Good luck!

I hope this helps! ~Alex

Also, get rid of the try/catch thingy. I know you used it for testing but it might cause an error in the Code Challenge (it probably would work in Python however)

P.S.: This code would pass but Python is picky and you call .index("a") and "a" isn't in the iterable then Python actually would give an error.