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

solution

can somebody help me with the solution.

trial.py
def add(one,two):
    try:
        float(one)
         float(two)
    except ValueError:
        return None
    else:
    return one+two

add(67,98)

1 Answer

Josh Keenan
Josh Keenan
20,315 Points

I will post my solution and explain it and then explain what's wrong with yours.

def add(item, other):
    try:
        return (float(item)+float(other))
    except ValueError:
        return None

The challenge wants us to return the float if it is possible, but if not return None. This code does that, it tries to return the total float number, if it can't then it returns none.

def add(one,two):
    try:
        float(one)
         float(two)          # <-- Indentation error here
    except ValueError:
        return None
    else:
    return one+two .    # <-- Another indentation error

So firstly you need to save the floats so you should save them as

one = float(one)
two = float(two)

This saves them so they can be called further in the function, without this they disappear and can't be reaccessed by the code.

If you have any questions let me know