Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Logic in Python Try and Except

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,920 Points

Try 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!

trial.py
def add(arg1, arg2):
    try:
        test = int(arg1) + int(arg2)
    except ValueError:
        return(None)
    else:
        return(float(arg1) + float(arg2))

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,623 Points

Hey 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))

:dizzy:

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,920 Points

Thanks, 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
Jason Anders
Treehouse Moderator 145,623 Points

The 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. :)