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

tyler bucasas
tyler bucasas
2,453 Points

trial.py task 3

i dont understand why is says task one no longer works. and i dont fully understand what the float() function does. pls help

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

1 Answer

Cameron Nilon
Cameron Nilon
15,601 Points

Hey Tyler,

first off float converts an integer say 1 or 2 to 1.0 or 2.0 Example:

number = 4 float(number) 4.0

For your problem add a try before your return and get rid of the else. Example:

def add(num1, num2):
    try:
        return #your logic goes here
    except ValueError:
        return #your logic goes here
Cameron Nilon
Cameron Nilon
15,601 Points

Also like to add, that float() can convert a string number value say '5' i.e. float('5') returns 5.0