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

Hi, I've passed the task 1 and 2. Task 3 gives me the error msg "It looks like Task 2 is no longer passing." Help please

...python

def add(a, b): try: a = int(a) b = int(b) except ValueError: return None else: a = float(a) b = float(b) return a + b ......... Is there anything wrong with this code? I dont seem to find it. A little tip will be very helpful Thank you

trial.py
def add(a, b):
    try:
        a = int(a)
        b = int(b)
    except ValueError:
        return None
    else:
        a = float(a)
        b = float(b)
        return a + b

2 Answers

Stuart Wright
Stuart Wright
41,120 Points

'Task 1 is no longer passing' usually means a syntax error, but in this case it actually doesn't. There's no syntax error in your code.

But your logic isn't quite right. You don't want to convert floats that you pass in to type integer. The changes you have to make are as follows:

  • inside your try: block, convert to float rather than int
  • remove the float conversion from your else: block
Aiden Colley
Aiden Colley
926 Points

What it means is that you have a syntax error somewhere that python doesn't understand. I'm not sure what the syntax error is but maybe if you try changing what kind of error your trying to catch.

Thanks for your suggestion and you were right!. I misunderstood the problem. I changed the type conversion in the "try block" and only return "the result" in the else block. It worked!! ^.^