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

need help

this is my code please tell me where i do mistake

trial.py
def add(x,y)
    try: 
        sum=return(float(x+y))
    except ValueError:
        return None
    else:
        return sum
add(5,5)

2 Answers

Jeff Hartl
seal-mask
.a{fill-rule:evenodd;}techdegree
Jeff Hartl
Python Web Development Techdegree Student 8,420 Points
  1. When you define a function there should be a colon.
  2. sum=return(float(x+y) is incorrect. Should there really be a return statement on that line, since later in your code you are returning sum?
  3. Else statements cannot run without an if statement higher up in the block of code. Since you are using a try\except block, think about whether you really need an else statement.
  4. Could the function throw more exceptions than just a ValueError?

thanks to sort out my doubt

Wairton Rebouças
Wairton Rebouças
8,225 Points

Just one minor correction to Jeff Harti's answer. In python it is possible to use and else statement for exception handling, see section 8.3 of https://docs.python.org/3/tutorial/errors.html

The challenge asks the user to use an else statement for a try except block so, a valid answer would be:

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

note that the challenge asks you to convert to float before the sum