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

C Miyauchi
C Miyauchi
3,084 Points

Can someone help me with instances.py?

I keep getting Bummer! Didn't get the expected output.

instances.py
def combiner(x):
    y=""
    z=0
    a=0
    for i in x:
        if isinstance(i, str):
            y+=i
        if isinstance(i, float):
             z+=i
        if isinstance(i, int):
             a+=float(i)
        b=z+a
    return "{}{}".format(y,b)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Good question! At first I thought it was due to the float use in the int addition, since if no floats were present in the input an unnecessary ".0" would be in the output. But removing this didn't pass the challenge. I considered what would happen if only ints or only floats were present, but the code handles this by starting z as an int. I've even tried unindenting the assignment to b since it's only needed after the loop completes, to no avail. There is a potential test case if no numbers were present, then a extraneous "0" would be printed. But the suggested fix in the next paragraph always adds a numeric suffix to the string output and it passes.

The straightforward solution would be to replace the checks for float and int with a simple else: to treat all non-strings as numeric. This passes the challenge.

What's not clear is why the explicit used of type checking for floats and ints causes it not to pass. I don't have access to the test data used in the challenge checker.

EDIT: See comment below. This code should pass, but does not due to floating point precision errors caused by the order in which the numbers are added.

Tagging Craig Dennis to check why this code doesn't pass:

def combiner(x):
    y=""
    z=0
    a=0
    for i in x:
        if isinstance(i, str):
            y += i
        if isinstance(i, float):
             z += i
        if isinstance(i, int):
             a += i
    b=z+a
    return "{}{}".format(y,b)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

With the help of Chris Howell (thanks!), I was able to drill down and figure out that the order in which the values are added may result in a different numeric value due to floating point precision errors. Case in point:

# floating point precision error
>>> 9.2 - 9.0 == 0.2
False
>>> 9.2 - 9.0
0.1999999999999993

# transitive error (due to cumulative round off)
# data if added in order of input
>>> sum([8.1, 2.2, 7])
17.3
# data if added in reverse order
>>> sum([7, 2.2, 8.1])
17.299999999999997

Suggestions have been made to update the challenge checker to use rounding so both answers are accepted.

C Miyauchi
C Miyauchi
3,084 Points

Thank you so much for you help!