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

William Davis
William Davis
10,070 Points

"Try and except" challenge not passing!

Hey there! I'm out of ideas as to why this isn't working. It's from the "Logic in Python" section of the Python basics course. Thanks!

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

1 Answer

Josh Keenan
Josh Keenan
20,315 Points

Here's my solution:

In the try block I change the value of num1 and num2 to float values, well I try to.... get it? I'm sorry..

Then in the except I return None as the challenge requested, not the string "None", the keyword None.

Then in the else if the conversion passes or fails it still returns the total of the numbers!

Does that make sense?

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