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

Return Added Floats

Hello,

I'm confused about what the floats do to my arguments. For the last challenge, I believe my code is correct up to the line else: Here's how it looks. def add(two, three) try: return float(two) + float(three) except ValueError: return(None) Else: return(two + three)

I'm unsure of how to correct this.

Thanks!

trial.py
def add(two, three):
try:    
    return float(two) + float(three)
except ValueError:
    return(None)
else:
    two + three 

Hi,

try the below:

def add(two, three): try:
two = float(two) three = float(three) except ValueError: return(None) else: return two + three

2 Answers

Bharath,

I tried that, but it didn't seem to work. I'm still confused about the floats. Can someone explain that to me?

Thanks,

Hi Andrew,

The float() function takes in an integer or a string argument and turns it into a floating point value, which is basically a number that contains a decimal point. For example: if you pass an integer value of 5 to the float(5) function, you will get back 5.0. Similarly, if you pass a string value float("5"), you will still get back 5.0. BTW I tried the code and it works for me. What happens when you are trying to run the code? Hope this is clear.

Thanks, Bharath