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 trialAssma Al-Adawi
8,723 Pointsinstances.py not working!
I am really at a loss here. I have seen some of the other answers but I just don't feel my solution is wrong. Can anyone offer any insight?
def combiner(list):
string = []
numbers = 0
for item in list:
if isinstance(item, (int, float, str)):
if type(item) == float or type(item) == int:
numbers += item
else:
string.append(item)
string.append(str(numbers))
final = "".join(string)
return final
1 Answer
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 Pointshi there, the only thing I see wrong with your code is that you are using a reserved word 'list' also it is preferable to use 'is' instead of '==' with the type function, having said that there is nothing that I can tell why this should not work. In my humble (i am still learning - so take it as you like) opinion doing a step too much. There is no need for the type() testing you have already done that with isinstance()
def combiner(my_list):
string = []
numbers = 0
for item in my_list:
if isinstance(item, (int, float)):
numbers += item
else:
string.append(item)
string.append(str(numbers))
final = "".join(string)
return final
Assma Al-Adawi
8,723 PointsAssma Al-Adawi
8,723 PointsThanks alot Stuart! I decided to take your advice and cleaned it up a notch.