Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,017 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).