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 trialSeamus Hannan
509 PointsCan't convert 'int' object to str implicitly
I know the issue is in my syntax around the " if item.index(0) == 'a' " part since the item isn't a string the program can check, but I cant figure out how to fix it.
I need items to be a list of things that I can check the first character of, so i try to make the program change the item name into a string on line 3 so on line 4 i can check the first character but it wont do it.
I tried running it in workspaces but the specific nature of the tutorials make it non-relatable.
def loopy(items):
for item in items:
str(item)
if item.index(0) == 'a':
continue
else:
print(item)
continue
2 Answers
andren
28,558 PointsActually the item is a string. The problem is 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':
continue
else:
print(item)
I removed the last continue from your else statement since it is unnecessary. The loop will automatically restart once it has run though all of its code without you having to call continue
. That keyword is only necessary when you want to skip code within the loop.
Stuart Wright
41,120 PointsYou have the right idea - the syntax is just wrong in the line that you've highlighted in your post.
The correct syntax for accessing the character at index 0 in a string is:
item[0]
Without changing anything else in your code, it passes the challenge:
def loopy(items):
for item in items:
str(item)
if item[0] == 'a':
continue
else:
print(item)
continue
Edit to add: didn't see there was already an answer provided. I agree with the improvements to the code made in the other answer.
Seamus Hannan
509 PointsSeamus Hannan
509 Pointssuch a good answer, thank you. I've been at this far too long. I knew it was a silly mistake. I only complete a few of these video sections at a time and as a result keep forgetting basic info and making this kind of error.
andren
28,558 Pointsandren
28,558 PointsDon't worry about it. It's pretty normal to make mistakes like this in the beginning. And on this task in particular its actually somewhat common for people to make this exact mistake. Both in terms of using the
index
function and in terms of using it incorrectly so no need to feel bad about that.The beginning is often the toughest part when it comes to learning programming. Once you have a decent understanding of the core concepts a lot of the more advanced stuff will often be far easier to learn than you might think.