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

Thomas Beaudry
Thomas Beaudry
29,084 Points

Something is not right, this is part 3 of "Try and Except"

Add a try block before where you turn your arguments into floats.

Then add an except to catch the possible ValueError, inside the except block.

Add an else for your final return.

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

3 Answers

Hi, This code should work. I think you should just add them in the try block before returning them.

def add(num1, num2):
    try:
        float(num1) + float(num2)
    except ValueError:
        return None
    else:
        return float(num1) + float(num2)
Jarred Miles
Jarred Miles
22,560 Points

Your code is right you just added () after return when they are not needed. Your code should look like this.

def add(num1, num2):
    try:
        float(num1) + float(num2)
    except ValueError:
        return None
    else:
        return float(num1) + float(num2)
Thomas Beaudry
Thomas Beaudry
29,084 Points

Jarred, I tried your code without the () but it's still showing "Bummer! Try again!)

Jarred Miles
Jarred Miles
22,560 Points

Oh yeah, my bad I did this real quick I see my error. >.< I'm glad you got your question answered though!

Thomas Beaudry
Thomas Beaudry
29,084 Points

Jarred Miles, thanks for ur input, glad you tried to help out, happy coding :)

Thomas Beaudry
Thomas Beaudry
29,084 Points

Hello Kevin Li, Yes, you got it, I should have just added them in the try block before returning. Your right it did work! Wow, I've been on this code challenge since Friday! You are THE MAN...thank you so much! :)

Yeah... no problem. I remember I was stuck on that for a week.