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

Prashant Tyagi
Prashant Tyagi
758 Points

Tree house editor doesn't pass, these solution. Kindly help me to understand what I am doing wrong

So if I try this code at my machine it doesnt work, but to complete the course I need to get this code pass by the tree house editor. And it says Try Again.

Question is: Right now, we turn everything into a float. That's great so long as we're getting numbers or numbers as a string. We should handle cases where we get a non-number though. Add a try block before where you turn your arguments into floats. Then add an except to catch the possible ValueError. Inside the except block, return None. If you're following the structure from the videos, add an else for your final return of the added floats.

And my answer is : def add(a1, a2): try: a1 = float(input("Please enter float 1")) a2 = float(input("Please enter float 2")) except ValueError: print("Not a float") else: a3 = (a1)+ (a2) return a3

Please refer attached .py file

trial.py
def add(a1, a2):
   try:
    a1 = float(input("Please enter float 1"))
    a2 = float(input("Please enter float 2"))
  except ValueError:
    print("Not a float")
  else: 
  a3 = float(a1) + float(a2)
  return a3

1 Answer

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

Hi there! You're doing fairly well, actually. But there's a couple of things going on here. The biggest one is this: you're overwriting what Treehouse is sending. Treehouse is going to send in numbers to your function to test it and those will be set to the values a1 and a2. But then you immediately take those and overwrite them with prompts to the user. And since we have no idea which numbers Treehouse is sending in our results likely will not be the same.

Secondly, the instructions explicitly state to return None in the except. But you're printing something.

Take a look at how I did it and see if it makes sense:

def add(x,y):
  try:
    return float(x) + float(y)
  except ValueError:
    return None

Here we define the function add and take two inputs (sent in by Treehouse). We will attempt to transform both of those values to a float. If either one cannot be converted (ie if Treehouse were to send in "Hello" and 2), the try will fail and go directly to the except with a ValueError. At this point we return None.

Prashant Tyagi
Prashant Tyagi
758 Points

Thanks Genius!! It worked. :)