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

Joshua Smith
Joshua Smith
3,843 Points

What's the proper manner to convert function arguments using the float() to ensure a final result of type float?

In the code on this post, I'm using Python and attempted to convert the arguments to type float in the definition of the add() function. Prior to that, I also attempted to convert them to float in the initialization of the (total) variable. Neither has worked for me :(

Any help or recommendation on how to properly do this would be appreciated!

trial.py
def add(float(num1, num2)):
    total = num1 + num2
    return total

 add(6, 7)

1 Answer

It is invalid code to do anything inside of the parentheses of a function definition except for arguments. And you used the float function in the parentheses, which is invalid.

Fix it by using it in the function code itself:

def add(x, y):
    return float(x) + float(y)

Also, you don't need to call the function; the code challenge calls it automatically.

I hope this helps. ~Alex

Joshua Smith
Joshua Smith
3,843 Points

I understand now, thanks Alex!

No problem :smile: