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

what is wrong in this code?

its a challenge task 3/3 for try and except. something is wrong with the else part which i am unable to figure out. help me out with the solution. thanks

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

2 Answers

3 important errors:

  1. Your indentation is way off.
  2. You forgot a colon in front of def add(num1, num2)
  3. There's no need for an else.

Fix all of that, your code should look like this:

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

Good luck! ~Alex

thanks alex. the code worked. small mistakes though in my code.

Your welcome :)