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 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.

trial.py
def add(apple, orange):
    try:
        apple = int(input("please input a number"))
        orange = int(input("please input a number"))
    except ValueError: 
        return None
    else:
        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):
  try:
    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.