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

I'm getting the "Bummer! Try again!" response- but I don't know where or what is wrong with the code?

I've tried a few different ways of writing the code- but I keep getting the same bummer error message with no direction on where the issue is

trial.py
def add (left, right):
try:
  return (left +right)
except ValueError:
  return ("None")
else:
  return (float(left) + float(right))

1 Answer

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey Ellie,

you were on the right track but your code has a few problems. Firstly you have to indent everything inside of your add method. Then you have to change your arguments to floats in the try block with the float method but not return them in the try block. If there is an error you want to catch it and return None, note that you have to return the keyword None and not the string "None". In the else block you can then just return the result of the addition of the floats.

Here is a working solution:

def add(left, right):
  try:
    left = float(left)
    right = float(right)
  except ValueError:
    return None
  else:
    return left + right

I hope that helps, if you have further questions feel free to ask, good luck! :)