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 trialSon-Hai Nguyen
2,481 PointsI need help on this ```combiner()``` exercise
Please have a look at my following method. I tested it on Worksplaces, it worked just fine just like the requirement, but somehow the Recheck work kept saying there's a TypeError: sequence item 0: expected str instance, list found
Is there anyone know what I missed?
Thank you!!!
def combiner(*args):
numLs = []
strLs = []
for a in args:
if isinstance(a, (int, float)):
numLs.append(a)
else:
strLs.append(a)
sumInt = str(sum(numLs))
strLs.append(sumInt)
return ''.join(strLs)
combiner("apple", 5.2, "dog", 8)
3 Answers
KRIS NIKOLAISEN
54,971 Points*args accepts a variable list of arguments (so there can be more than one). The challenge will pass in a single list (any number of items but they will be enclosed in brackets so there will only be one list)
The easiest fix to your code is just remove the * from your parameter
KRIS NIKOLAISEN
54,971 PointsThe function takes a single argument. If you want to test in a workspace try:
print(combiner(["apple", 5.2, "dog", 8]))
Son-Hai Nguyen
2,481 PointsThanks Kris, for your answer. But what does it mean? Doesnt the *args
stand for a list of arguments? How to iterate through it (since it's still a list right? I saw the [] here) if it's a single argument?
KRIS NIKOLAISEN
54,971 PointsIf you add print statements you better see what is going on:
def combiner(*args):
numLs = []
strLs = []
for a in args:
print(a)
if isinstance(a, (int, float)):
numLs.append(a)
else:
strLs.append(a)
sumInt = str(sum(numLs))
strLs.append(sumInt)
print(strLs)
return ''.join(strLs)
print(combiner(["apple", 5.2, "dog", 8]))
In the loop a is a list so since it since it isn't an int or float it is appended to strLs. Since that is the only argument there are no numbers to sum so sumInt = '0'. Append this to strLs you end up with [['apple', 5.2, 'dog', 8], '0']. At which point using join on a list and a string causes the error.
Son-Hai Nguyen
2,481 PointsSon-Hai Nguyen
2,481 PointsGreat!!! It sounds weird to pass all variables in as a single list anw