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

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
215,959 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