Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Logic in Python Try and Except

Sean Frye
Sean Frye
1,167 Points

I'm really trying here! I can't get this down!

I'm really trying here! I don't ever recall using the float function before or especially even learning about it during any part of this course and all I can do is look up what the function float even is and after reading about it and even typing something in, I still can't do it! I hate feeling like I'm not up to the task!

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

1 Answer

Cameron Nilon
Cameron Nilon
15,601 Points

Hey Sean,

Don't beat your self up about it, it will come to you. So what your trying to achieve is convert the type. So let's say you pass two numbers but they are in the form of a string type an example would be ~ add("1", "2") you will need to convert these two sting types into number types, in your case they are to be of float type. I think I can best explain this in code so here is an example to drive this home for you.

def add(num1, num2)
   return float(num1) + float(num2)

# some use examples
# add("1", "2") this will return 3.0
# add(5, 10) returns 15.0 

For some more info on this topic look into type casting. This is generally used accross all different programming languages.