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

Not sure what I did wrong

The only error message I get is "Bummer: Try again!", but I don't know why my code wouldn't work.

instances.py
def combiner(list):
    finalStr
    finalNum
    for item in list:
        if isinstance(item, (int, float)):
            finalNum += item
        else if isinstance(item, str):
            finalStr += item
    return finalStr + finalNum

UPDATE:

my code works when I remove the "else" that wasn't needed.

1 Answer

In Python we use three keywords for if statements. They are:

  • if
  • elif
  • else

if is used first, then elif is used. You can have as many elif's as you want in an if statement.

For Example:

x = 4
if x == 1:
    print("")
elif x == 2:
    print("")
elif x == 3:
    print("")
elif x == 4:
    print("")
else:
    print("")

Don't forget elif and else is optional!

So you code would look like this

def combiner(list):
    finalStr = ""
    finalNum = 0
    for item in list:
        if isinstance(item, (int, float)):
            finalNum += item
        elif isinstance(item, str):
            finalStr += item
    return finalStr + str(finalNum)

Other changes:

  • Initialized the variables
  • Converted finalNum to a string for the return