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

Anne Manning
Anne Manning
5,415 Points

I don't know how to solve this assignment.

I am having trouble with the try and except blocks of code in challenge 3 of 3. any direction would be great.

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

3 Answers

One way to make your code work would be to move the try-except block inside the add function (you also don't need the else clause):

def add(a, b):
  try:
    fl_a = float(a)
    fl_b = float(b)
  except ValueError:
    return None
  return fl_a + fl_b
Anne Manning
Anne Manning
5,415 Points

unfortunately, I am still getting the error message: "Bummer, try again"

Gianmarco Mazzoran
Gianmarco Mazzoran
22,076 Points

Hi,

i think you're not passing the last task because it asking you to add the try: method before you convert your arguments into floats. In your code snippet you convert a and b and return them two times.

Instead you could do this to keep the things clean:

tryal.py
def add(arg1, arg2):
    try:
        return float(arg1) + float(arg2)
    except ValueError:
        return None

Anne,

I just tried the code I suggested, and it passed the test. Make sure the indentation levels are consistent with the code above (try, except, last return lines must have same indentation).