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

Paul Sirota
Paul Sirota
1,514 Points

what is my problem?!

I can't find what is wrong here.

trial.py
def add(x,y):
  try:
        x = int(raw_input("Please enter a number for x: "))
    except ValueError:
        print("Oops!  That was no valid number. Try again...")
        y = int(raw_input("Please enter a number for y: "))
    except ValueError:
        print("Oops!  That was no valid number. Try again...")
        else:
            return float(x)+float(y)

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Well, for starters, you're overthinking this. You've got two separate exceptions when there's a ValueError. Also, it wants you to return the sum of the result of float(x) and float(y). But here you're trying to return that in the exception, which is where that should have failed already. We should first try to return that then if that fails we return None (as per the challenge instructions). Here's my code for clarification:

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

I take the two numbers and convert them to float and add them. But if that fails with a ValueError exception, I simply return None. Hope that makes a little more sense now!