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

what is the correct syntax for try and except with two arguments that should return a float

Can someone take a look at my structure for this code challenge, I am not sure what is causing this challenge to fail.

def add(apple, orange):
        apple = int(input("please input a number"))
        orange = int(input("please input a number"))
    except ValueError: 
        return None
        return float(apple) + float(orange)

The problem is in your try and else statements.

you need to convert your apple and orange to floats in the try. You can scratch everything that is in the try right now. And instead copy and paste your else into your try like so

def add(apple, orange):
    final = float(apple) + float(orange)

You can then return final in else the except stays in the middle as is though

1 Answer

Thanks I got it! I got messed up when I was trying to evaluate the arguments not the results. also to note that you do not need to declare a variable in the try block for the challenge to pass though it does make sense for dry purposes, the last expression in your block can be return final instead of repeating your expression.