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

Andrew McMaster
Andrew McMaster
1,377 Points

Ensuring arguments are integers

Having a problem understand task 3 of 3. I have to ensure the arguments are numbers. I think the line that is wrong is add = int().

Any pointers?

trial.py
def add(num1, num2):
    try:
        add = int()
        except ValueError:
            return None
        else:
            return float(num1) + float(num2)

2 Answers

Derek Woods
Derek Woods
6,272 Points

You're pretty close, try, except and else need to all be at the same indentation level. Also I just put the return equation in the try section and didn't use the else: it still passes

Derek Woods
Derek Woods
6,272 Points

Just to add on it will "try" to add the floats and return the answer and if there is a ValueError because you are trying to add a string or whatnot it will go to the "except ValueError:" section and return None

Andrew McMaster
Andrew McMaster
1,377 Points

Mmmm. Still not able to get this to pass.. I have the following:

def add(num1, num2): try: add = int() except ValueError: return None else: return float(num1) + float(num2)

It now states the following error:

ValueError: could not convert string to float: 'a'

Derek Woods
Derek Woods
6,272 Points

try putting what is in else in the try. try: return float(num1) + float(num2) except ValueError: return none and leave out the else