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

Can instances.py work this way?

I want to solve this challenge using only a single list, and then the combiner function iterates over each item in the list, doing what's needed. I'm getting syntax errors in workspaces in my conditional statement.

The the output from workspaces:

  File "combiner.py", line 5                                                               
    return the_list.join(items) as strings                                                 
                                 ^                                                         
SyntaxError: invalid syntax

Can I even make it work this way? The instructions call for a single list as an argument, but it seems like I might have to set a separate number variable, which I've seen in other solutions.

Any guidance is greatly appreciated!

instances.py
def combiner(the_list):
    the_list = ["pen", 1, "apple", 2, "pineapple", 3]
    for item in the_list:
        if isinstance(item, str):
           return the_list.join(items) as strings
        elif isinstance(item, (int, float))
           return sum(items) as numbers

    result = join(strings, numbers)
    print(result)

3 Answers

Steven Parker
Steven Parker
229,732 Points

Here's a few hints:

  • "the_list" is being passed in as an argument, you won't want to assign it to a literal value
  • if you "return" inside a loop, the function will end before all items are handled
  • you don't need the aliasing operator ("as") in the return statement
  • using "join" on a list creates a single string
  • the "join" method takes only one argument
  • the function needs to "return" the result
  • you won't need to "print" anything

Hey Steven. Trying to implement your hints here. I removed the return statements in the loop and have only 1 at the end. I'm getting this feedback:

"TypeError: 'int' object is not iterable"

If I'm joining strings and numbers that are contained in a single list, how can I reorder them? (assuming they are not already in order)

def combiner(the_list):
    for item in the_list:
        if isinstance(item, str):
            join(str(item))
        elif isinstance(item, int):
            sum(item)
    return join(the_list)
Steven Parker
Steven Parker
229,732 Points

A few more hints:

  • you'll want to review the syntax for "join" — it needs to be called on a string and passed a list
  • you might consider simple concatenation to build up a string
  • you might need some temporary variables for collecting a sum and strings

I really appreciate all your hints Steven, but I don't think I follow this. I'm lacking context here, as the instructions are confusing to me.

Alright, here's a fun task!

Create a function named combiner that takes a single argument, which will be a list made up of strings and numbers.

Return a single string that is a combination of all of the strings in the list and then the sum of all of the numbers. For example, with the input ["apple", 5.2, "dog", 8], combiner would return "appledog13.2". Be sure to use isinstance to solve this as I might try to trick you.

I'm trying to follow these instructions as literally as I can, bc it's served me well in past courses. Clearly temporary variables were not something I even considered.

Why do I need temp variables? What is string.append(item) doing in the below solution? How does it contribute to result?

def combiner(the_list):
    number = 0
    string = []
    for item in the_list:
        if isinstance(item, int):
            number = number + item
        elif isinstance(item, str):
            string.append(item)
    result = "{}{}".format("".join(string),number)
    return result

Thanks so much for all your attention.

Steven Parker
Steven Parker
229,732 Points

In that solution, the temporary variables "number" and "string" are being used to accumulate the list elements. The "append" method adds an item to the "string" list which is later joined into the final value. Similarly, "number" provides an accumulator for summing the numeric values.

I ended up doing this. I am not sure if it the easiest way, but it worked. I found the length of the list, and looped through adding one until the count was equal to the length. In each loop, i check to see if argument was a str, float or int, and added it to the appropriate variable. Then I returned the string all combined.

It took me a while to figure this one out, and got a lot of help reading the Community Posts.

def combiner(the_list): num = 0 string = "" count = 0 length = len(the_list)

while count < length:
    if isinstance(the_list[count], str):
        string = string + the_list[count]
        count = count + 1
    if isinstance(the_list[count], (float, int)):
        num = num + the_list[count]
        count = count + 1
    if count == length:
        return "{}{}".format(string, str(num))

print(combiner(["apple", 5.2, "dog", 8]))

Steven Parker
Steven Parker
229,732 Points

One improvement might be to use "for...in" syntax instead of "while". That would save you from needing to index into the original list or managing a separate "count".

But congratulations on solving it! :+1:

Anupam Kumar
Anupam Kumar
3,795 Points

What is wrong with this code, I feel this is what being asked

def combiner(strin):
    stri=[]
    num=0
    for i in val:
        if isinstance(i, str)==True:
            stri.append(i)
        if isinstance(i, (int, float))==True:
            num+=i
    return ''.join(stri)+str(num) ```
Steven Parker
Steven Parker
229,732 Points

You've posted this as an "answer" to Michael's question, but it's not really an answer and won't be readily seen by other students here.

Instead, start a fresh question and include your code and a link to the course page with it.