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 trialSalvatore Architetto
1,506 PointsNeed help with Combiner - OOP
Im not sure what im doing wrong here, testing for Int, str, and float. Any help would be appreciated!
def combiner(string):
num = 0
string1 = []
for x in string:
if x isinstance(x, int):
num += x
elif x isinstance(x , float):
num += x
else:
if x isinstance(x, str):
string1.append(x)
return "{}{}".format(str(num), string1)
1 Answer
Rich Zimmerman
24,063 PointsThe isinstance method returns a boolean so you don't need to have it say
if x isinstance(x, int):
You only need
if isinstance(x, int):
You also have a mistake in your Else: part of the statement.
else:
if x isinstance(x, str):
string1.append(x)
You don't need the if, you could add an extra Elif to test for string and then continue in the 'Else' should a datatype be given that is not an int, str, or float.