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 trialC Miyauchi
3,084 PointsCan someone help me with instances.py?
I keep getting Bummer! Didn't get the expected output.
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
Treehouse Moderator 68,457 PointsGood 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)
C Miyauchi
3,084 PointsThank you so much for you help!
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsWith 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:
Suggestions have been made to update the challenge checker to use rounding so both answers are accepted.