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

Zachary Martin
Zachary Martin
3,545 Points

Challange 3/3 Try except

I looked in others with the same problem but I couldn't find the answer. Whats wrong with my code why won't it run?

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

add(2,3)
Zachary Martin
Zachary Martin
3,545 Points

I've tried moving the floats and other things around from the standard nothing worked.

1 Answer

Cheo R
Cheo R
37,150 Points

Hello Zachary, you're on the right track.

def add (n1,n2):   
try:    
              a = float(n1)
              b = float(n2)
except ValueError:
    return: None
else:    
    return(a+b)

Just a few changes will get your code working:

  1. indent your code properly.
  2. No colons are needed after the return keyword.
def add (n1,n2):   
    try:
        a = float(n1)
        b = float(n2)
    except ValueError:
        return None
    else:    
        return a+b

add(2,3)