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 Smith
Andrew Smith
10,947 Points

Quiz: Try and Except, Q2 - How to convert Python function return to float

First part of question:

I need you to make a new function, add. add should take two arguments, add them together, and return the total.

Second part of the question (which I can't figure out)

Let's make sure we're always working with floats. Convert your arguments to floats before you add them together. You can do this with the float() function.

trial.py
def add(one, two):
    return(one + two)

3 Answers

Russell Sawyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Russell Sawyer
Front End Web Development Techdegree Student 15,705 Points

For you to return the floated sum of the variables one and two you would need to write it like this.

return float(one+two)

or you could write it like this.

return float(one) + float(two)
Andrew Smith
Andrew Smith
10,947 Points

thanks, the second version worked, though the first one still returned incorrect

Andrew Smith
Andrew Smith
10,947 Points

How do I use Float() to convert the argument though? I tried this originally and it did not work.

def add(one, two):
    float(return(one + two))

def add(num1, num2): return float(num1) + float(num2) total = add(3,5)