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

Emilio Andere
Emilio Andere
495 Points

what is wrong now?

what is wrong here

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

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're really close here. There's only one line that's incorrect and it's this one:

  add = (num1 + num2)

You started by trying to see if the num1 and num2 could be converted to numbers and then assigned them to a and b, but then you never did anything with a and b. Also, this code would try to take the numbers passed in and add them without first converting them and then assign them to a variable named add, but that's what your function is named!

So if I switch out that line for this line:

  return a + b

your code passes! Hope this helps! :sparkles:

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Steven Parker is correct, you can remove the line and not replace it and pass. My line that I posted will work, but also gives you the option to erase the else clause at the end.

Steven Parker
Steven Parker
231,269 Points

Didn't you just ask another question about this?

As I said before, that line that creates the variable named "add" doesn't seem to be related to the challenge. If you eliminate that line completely you should pass the challenge (you don't need to replace it with anything).