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

Try and Except: objective 3 - cannot see where I have gone wrong

The following code does not work for Try and Except: objective 3.

def add(arg1, arg2): try:
arg1 = float(arg1) arg2 = float(arg2) except ValueError: return = None else: return = (arg1 + arg2)

I passed the former objectives with the same code without the try block. can anyone tell me where I have gone wrong?

trial.py
def add(arg1, arg2):
try:    
    arg1 = float(arg1)
    arg2 = float(arg2)
except ValueError:
    return = None
else:
    return = (arg1 + arg2)

3 Answers

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

Hi there! You're doing well, but there are a couple of things going on here. First, your indentation is off. All of these lines should be indented inside the function. Keeping the same indentation otherwise, you should indent every line below def add(arg1, arg2):.

Also, you have two syntax errors. These are found in your return statements. In both cases, you have an unnecessary equals sign there as if you're trying to assign a value to return. To return the value of the variable x, we would simply write return x.

When I correct these two things, your code passes! :sparkles:

You are very close and secondly you cannot follow a return statement with '='

def add(arg1, arg2):
    try:
        return float(arg1) + float(arg2)
    except ValueError:
        return None
    else:
        print(arg1 + arg2)
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi James Reinhold! I changed your comment to an answer. This has the benefit of marking the question as "answered" in the Community and also allows for voting on your answer. Thanks for helping out in the Community! :sparkles:

Thanks, I have completed the challenge