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 trialMathew Yangang
4,433 Pointsnot sure why my code not passing
Not sure why my code not passing
def combiner(lst):
x = ""
j = 0
for item in lst:
isinstance(item[lst],str):
item[lst] += item[lst]
isinstance(item[lst],float):
item += int[item]
item += item
return item
Mathew Yangang
4,433 PointsThank You Jen, very helpful
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsHi Matthew. There were several issues with your code
- x should be an empty list [] so that strings found can be appended to it. After the
for
loop, this list can be joined into a string - an if should come before the
isinstance
function. - the
isinstance
function should takeitem
as it's argument notitem[lst]
- Instead of += to concatenated partial strings. It is better to append to a list, such as
x.append(item)
. then use"".join(x)
to get a string from those items after thefor
loop completes - you test for type
float
but not for typeint
. Add anotherif
statement forisinstance(item, int)
. - both the float and int checks can use the same
j += item
to accumulate the numeric total - the return statement is intended to far. It should be outside of the
for
loop. As it is, it will return after the end of the first iteration of thefor
loop - the return statement should return the list x and j both converted to a string
I know that's a lot of comments, but it's helpful to understand each one. Please post back if you have any more questions. Good Luck!!
Jen Farrant
6,905 PointsJen Farrant
6,905 Pointsyou are not adding to x and j, which I think is your string and total so += should link to x or j
also, it can be helpful to use variable names which mean something, as it makes your code easier to read.
So x could be called words and j could be called numbers