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

How to get numbers to float before adding them

I tried adding the float before the n1 .float((n1 + n2)) and did not work and tried this: def add(n1,n2): return .float((n1)) + .float((n2)) add(5,5) still not working where do I add the float?

trial.py
def add(n1,n2):
    return .float((n1)) + .float((n2))
add(5,5)

3 Answers

Steven Parker
Steven Parker
230,995 Points

You've got the right idea, but:

  • there should be no period before the word "float"
  • you only need to define the function, you don't need to call it yourself
  • you only need one set of parentheses after each "float"

ah ok. This is a bit different from JS I'm so used to putting "." before a function Thanks

Steven Parker
Steven Parker
230,995 Points

Even in JavaScript you wouldn't put a period before an ordinary function name, but you might put a period between an object name and a method name. Later you will discover that Python also has methods, and uses the same notation style (period in between).

And yes, Python is pretty different from most other languages you've probably used before. The key is to learn how to "think in Python"! :smile:

yeah thats what I meant. So what about the this? Its saying to use the try stmt if we dont get a number and the except stmt, but what would i put in the try? def add(n1,n2): try: except ValueError(return None) else: return float(n1) + float(n2) add(5,5)

Steven Parker
Steven Parker
230,995 Points

To be sure your posted code is displayed correctly (with indentations, etc) you must use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

But even looking at the scrambled code, I would remind you that you only will define the function, not call it. And for the exception handling to work, the float conversions must be done in the "try" section of the code.