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

Instances.py Challenge

I can't figure out what I'm doing wrong. Tried running this in the work space and nothing would print. Plz help

UPDATE: I modified my code, but I still seem to be getting an error

instances.py
def combiner(arg):
    strings, numbers = [], []
        for item in arg:
            if isinstance(item, str): 
                strings.append(item)
            elif isinstance(item, (int, float)):
                numbers.append(item) 
        numbers = sum(numbers)
        strings = "".join(strings)
        return strings + str(numbers)

I think you need to return a value that is a string of both the strings in the list, and the numbers all together as a string value :star:

Be careful of where you create lists, only use them in a loop if you are doing something particularly peculiar. Place variables to sum, concatenate, append and etc etc outside of the loop.

This:

def combiner(arg):
    strings, numbers = [], []
    for item in arg:
        if isinstance(item, str):
            strings.append(item)
        elif isinstance(item, (int, float)):
            numbers.append(item)
    numbers, strings = sum(numbers), "".join(strings)
    return strings + str(numbers)

Is the same as this:

def combiner(arg):
    strings = []
    numbers = []
    for item in arg:
        if isinstance(item, str):
            strings.append(item)
        elif isinstance(item, (int, float)):
            numbers.append(item)
    numbers = sum(numbers)
    strings = "".join(strings)
    return strings + str(numbers)

:star:

  • Variable total has not been created, I think you want to use numbers
  • Indentation error for the for loop, it needs to be below the first line, also indent
  • join only takes one value
  • string is not defined, you want to use strings

Here I fixed your code.

try:

def combiner(argument):
    strings, numbers = [], 0
    for item in argument:
        if isinstance(item, str): 
            strings.append(item)
        elif isinstance(item, (int, float)):
                numbers += item 
    return "{}{}".format("".join(strings), numbers)

:star:

Carla your solution helped me a lot. I was still having issues because of improper spacing. It was hard to see without being in the proper IDE. THANK YOU

You are indenting the for loop, and everything in it.

Np William, always happy to help, oh, and it's Carla :).

Whoops, sorry.

Thanks again, Carla

2 Answers

Cheo R
Cheo R
37,150 Points

I added some print statements to show you what I mean. For every item in your argument list, you're creating a new list and variable for your totals.

def combiner(argument):
    print(F"Argument list: {argument}")
    for item in argument:
        new_list = []  
        total = 0
        print(F"\nItem: {item}\nnew_list: {new_list}\ntotal:{total}") 
        if isinstance(item, str): 
            new_list.append(item)
            print("This is a string: {}".format(item))
        elif isinstance(item, (int)):
            total += item 
        elif isinstance(item, (float)):
            total += item  
    new_list.append(total)
    print(F"new_list out of the loop before print: {new_list}")
    print("".join(new_list))

which prints:

Argument list: ['apple', 5.2, 'dog', 8]

Item: apple
new_list: []
total:0
This is a string: apple

Item: 5.2
new_list: []
total:0

Item: dog
new_list: []
total:0
This is a string: dog

Item: 8
new_list: []
total:0
new_list out of the loop before print: [8]
Traceback (most recent call last):
  File "main.py", line 29, in <module>
    print(combiner(["apple", 5.2, "dog", 8]))
  File "main.py", line 16, in combiner
    print("".join(new_list))
TypeError: sequence item 0: expected str instance, int found

Let's address each problem:

    for item in argument:
        new_list = []  
        total = 0

You end up with a new_list, total, variable for each item in your loop.

 if isinstance(item, str): 

In this exercise, because they're not explicitly checking for int/floats, you could just check whether an item is a string or not.

    new_list.append(total)
    print("".join(new_list))

Because the last item is an int, you get an error when trying to join your list of strings (which don't exist because as mentioned before, you're creating a new list for each item in the list. Your final list only has an int).

You also need to return the last list, joined.

Thank you. I updated the code, but I'm still getting an error

Cheo R
Cheo R
37,150 Points

What errors are you getting? I ran some tests and these are the results:

UnboundLocalError: local variable 'total' referenced before assignment

NameError: name 'string' is not defined

TypeError: join() takes exactly one argument (2 given)

Copying your updated code, I got these errors. Can you figure out why I'm getting these three errors? Solve these three errors, and you should be good to go.