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 not sure what i'm doing wrong on this challenge.

I don't know if we were supposed to create an input to get the numbers? It doesn't say that we need to, but I can't get the correct answer here. If anyone can explain what i'm doing wrong, i'd appreciate it.

Thanksm

trial.py
def add(one, two):
  try:
    one = float(one)
    two = float(two)
  except ValueError:
    return ("None.")
  else:
    return (one + two)

ad = add(3,3)
print(ad)

1 Answer

Steven Parker
Steven Parker
231,248 Points

You're really close! Here are a few hints:

  • for the challenge, just define the function, don't call it yourself
  • you also won't need to print anything
  • None is a special Python keyword. Don't add a period or put quotes around it (that makes it an ordinary string).
  • FYI: you don't need parentheses after a return (but they don't hurt anything)

ahh! Thank you. I got it now.

Hi Steven, got the same error as Frank here but I'm not sure why since I've wrote it in the right manner

def add(one,two):

try:

numbers = float(one) + float(two)

except ValueError: return None else: return numbers