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

question not understood ?? why is this not correct; see code

I seem to spend all of my time trying to understand the questions, the still seem to be written in a way that I can not comprehend.....

def add(arg1, arg2): try: float(arg1), float(arg2) except ValueError: return('None') else: return (float(arg1) + float(arg2))

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

2 Answers

Seth Reece
Seth Reece
32,867 Points

Hi barrysw,

In your try block, you want to convert you args to float. You could do arg1 = float(arg1) or total = float(arg1) + float(agr2) Also, you don't need the extra parentheses in your return statement. return arg1 + arg2 should work fine if you convert them to floats in you try block.

Fabian George
PLUS
Fabian George
Courses Plus Student 11,133 Points

Hey barrysw,

I was having the same problem. Only thing you need to do to make your code work is to remove the parentheses and single quotes around the None.

def add(arg1, arg2):
    try:
        float(arg1), float(arg2)
    except ValueError:
        return None
    else:
        return (float(arg1) + float(arg2))