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

Skylar West
Skylar West
564 Points

Python: when to use try

Been banging my head against this function for a while. I've managed to make the function work multiple ways up until I add the try block and it's exception return. Also is this the best way to change the arguments into floats? Someone please help and maybe share the correct liens so I can compare and understand what I'm doing wrong. Thank you!

trial.py
def add(butt, head):
try:
    total = (float(butt) + float(head))
except ValueError:
    return (None)
else:
    return (total)

add(5, 4)

1 Answer

Welcome to the wonderful world of Python! This language will let you get away with a lot, but one thing that matters is spacing. Try adding spacing to the try, except, and else blocks so it looks like this.

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

EDIT: Per Chris' suggestion, changed to using spaces instead of tabs!