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

Stefan Vaziri
Stefan Vaziri
17,453 Points

Help!

Not sure how to do the "return of the added floats." Also I don't think I'm turning everything into a float. I used the same thing from line 2 but I don't think that's right.

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

2 Answers

get rid of the first part of your code. we need to "try" to convert the numbers before actually adding them. if we can convert them then we return the added value, otherwise throw exception. and then return 'total' at the end if no errors are thrown

def add(num1, num2):
  try:
    total = float(num1) + float(num2)
  except ValueError:
     return None
  else:
    return total