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 trial

Python

Frances Angulo
Frances Angulo
5,311 Points

isinstance challenge

I can't think of how to relate the recent content to this challenge. When I look through the questions asked and solutions presented by students there are a lot of concepts that I don't think I've been exposed to yet. Can I get some direction on this?

instances.py
the_list = ["apple", 5.2, "dog", 8]

def combiner(inventory): 
  for item in the_list:
    print(item)

combiner(the_list)

1 Answer

You have an OK start but maybe this will help you get going:

  • Your parameter is inventory so you should iterate over that instead of the_list
  • Go through this quick tutorial on isinstance
  • Inside the function create two variables: one for numbers and one for strings
  • Since isinstance returns true or false you could use if conditions to determine if you have a number or string while iterating
  • Depending on the result update your variables; concatenation for strings or addition for numbers. Hint += can be used for both with different results depending on the variable type.
  • After your loop concatenate the two variables and return
Spencer Hurrle
Spencer Hurrle
3,128 Points

Why don't we use *args for this? Would that also work as the argument? I thought if Kenneth was giving us a list as an argument, the argument would have to be *args... I am so confused!