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

Maggie Wolff
Maggie Wolff
495 Points

python basics try and except challenge task 2 of 3

I have re-watched the video, and have gone through my notes, and i still can't figure out how to answer.

'''num1 = float(num1) num2 = float(num2) def add(num1, num2): return num1+num2'''

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

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Maggie,

It seems that you may have misinterpreted what the Task 2 instructions are asking. It is asking you to

Convert your arguments to floats before you add them together.

Instead, you have created new variables with the same name as the arguments in the function. These are actually two different things.

You have the right idea on what needs to be done, just not quite in the right place. You need to convert the arguments to floats inside of the function. You already have the correct code block to add, now you just need to 'combine' what you did above to what is in the function, like this:

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

Hope that helps and makes sense. :)
Keep Coding! :dizzy:

EDIT: After reading what needs to be done for Task 3, the above code, while correct, isn't what should be used. So, you have all the correct code, just move those first two lines inside of the function. So, I guess for task 2, you'll should use the more verbose (least DRY) code of:

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

Sorry.

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Kenneth Love

After reading Task 3's instructions, I realized the DRY code may confuse newer students when trying to complete Task 3. I edited my answer above to reflect this, but could you have a look... maybe the DRY code should get a Bummer! for Task 2, as it isn't exactly what is being asked for. What do you think?

Jason

Maggie Wolff
Maggie Wolff
495 Points

I have another question. I tried to figure out how to put in the "try" and "except" blocks. It didn't work, and I don't really know what to do...