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!

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 Python Basics (2015) Shopping List App Continue

Can't convert 'int' object to str implicitly

Seems simple to me but I get the above error.

def loopy(items):
    for item in items:
        if item.index(0) == "a":
        print (item)

2 Answers

28,552 Points

The error come from the fact that you are using the index function incorrectly. It does not take an index and return a character, it does the exact opposite. You are meant to provide it with a character and then it returns the index of said character if it exists, or throws an exception if it does not.

It's actually not a good idea to use the index function for this task. The index function is more suited for situations where you expect a character to always be present. That's why it actually throws an exception if the character is not found.

To check the first index of a string you can use the exact same technique you used in an earlier challenge to check the first item of a list (bracket notation). This is due to the fact that in Python strings are treated as lists of characters.

So you can solve the challenge like this:

def loopy(items):
    for item in items:
        if item[0] == 'a':

Which as you can see is pretty close to the code you had, it just uses bracket notation to get the first character rather than using the index method like you tried to do.

Thank you very much!