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

Julia Swavola
Julia Swavola
413 Points

wherever I add the try block, I get the error that Task 1 is no longer working

the prompt specifies that a try block should be added above where I return the added floats. But I cannot find a place to add the try block that does not ruin the code that it's expecting to read.

trial.py
    def add(num1,num2):
        try:
            num1 = float()
            num2 = float()
        except ValueError:
            return None
    else:
        return (float(num1)+float(num2))
    add(2,3)

Hey Julia!

Well a couple of things here. First of all, you do not say:

num1 = float() # This is in fact saying that num1 == 0.0

Instead go ahead and say:

num1 = float(num1)

Secondly you do not need to say return (float(num1)+float(num2)) seeing as we have already converted them. Instead just add them as they already are. Finally im not sure what you're trying to do with your final statement:

add(2,3) # This dosent make any sense

At the end you should have something looking like this:

def add(num1, num2):
    try:
        num1 = float(num1)
        num2 = float(num2)
    except ValueError:
        return None
    else:
        return num1 + num2

Hope this helps!

2 Answers

Accidentally posted that as a comment :P

Julia Swavola
Julia Swavola
413 Points

Thank you so much. That all makes sense and it worked when I followed your instructions.