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 trialMatt Duarte
2,497 PointsHow 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?
def add(n1,n2):
return .float((n1)) + .float((n2))
add(5,5)
3 Answers
Steven Parker
231,271 PointsYou'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"
Matt Duarte
2,497 Pointsah ok. This is a bit different from JS I'm so used to putting "." before a function Thanks
Steven Parker
231,271 PointsEven 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"!
Matt Duarte
2,497 Pointsyeah 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
231,271 PointsTo 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. 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.