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 trialChris Jones
Java Web Development Techdegree Graduate 23,933 PointsTry and Except Code Challenge (Python Basics): Why isn't this code passing the code challenge?
Can anyone tell me why this code isn't passing the 3rd part of the "Try and Except" code challenge for Python Basics? I keep getting "Oops! It looks like Task 2 is no longer passing." I've read through the challenge description several times now and it seems like this code should pass the code challenge. Can someone help?
Thank you!
def add(arg1, arg2):
try:
test = int(arg1) + int(arg2)
except ValueError:
return(None)
else:
return(float(arg1) + float(arg2))
3 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHey Chris,
The try block
is checking to make sure things are changed to "floats," which you must have had as you passed Task 2. But now you seem to have "Int" instead, which is causing the "Bummer." If you change your try block
back to changing the input to floats ( with the float() ) function, it all passes.
def add(arg1, arg2):
try:
test = float(arg1) + float(arg2)
except ValueError:
return(None)
else:
return(float(arg1) + float(arg2))
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsThanks, Jason! That worked!
Just curious: The code challenge was asking me to make sure the functions arguments were numbers. Isn't that what I was doing with the Int function? Is this just an example of there being multiple ways to pass the code challenge, but only one being the way that the code challenge was looking for?
Thanks, again!
Jason Anders
Treehouse Moderator 145,860 PointsThe second part of the challenge says: "Let's make sure we're always working with floats." Yes, technically Floats and Ints are numbers, but in coding, they are two very different things. An Int (or Integer) is a whole number only, so 1, 25, 634, 3544 are Ints, but Floats (Floating points) are numbers with a decimal, so 23.53, 2.0 (not the same as 2), 1.4 are Floats. That's why float(arg1)
works but Int(arg1)
doesn't.
So, in a sense, it is just another way to pass the challenge, but both ways will work very differently depending on the values being passed in.
Hope it makes sense. :)
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsIt does! Thanks for your help!!