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

Windy Elliott
Windy Elliott
776 Points

I don't understand why this code isn't working and passing task 3.

how do you debug inside the challenges? I don't know why this code isn't working. Is it looking for a different format?

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

add("one",4)

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

add("one",4)

3 Answers

The only thing that I can see is your last line.

add("one",4)

It will raise a ValueError and would prevent your code from passing if you have it in the challenge. This code passes fine for me. The only other note that I make is that return doesn't require (and it's not Python convention) for parentheses to be around the value(s) that it's returning.

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

how do you debug inside the challenges?

I usually try to write the code in a IDE. AN IDE like PyCharm will naturally correct most of your syntax errors. I bought Pycharm just to do this course, it works great at getting you to write PEP8 standard. https://www.python.org/dev/peps/pep-0008/

This is one of my favorite things about Python, it's a beautifully clean and easy language read and write. Ever try javascript? ;-)

The best way is to copy and paste the code into an IDE and run it there. There isn't a way I know of to run a debugger in the challenges.

I've written a bit of javascript, java, and Swift. The syntax drives me crazy. I started on Python and I don't want to switch.

My comment to JavaScript was purely a jab at how much punctuation is required. Its messy to write. Just my opinion. Python makes me happier.