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

Nicolas Tofino
Nicolas Tofino
416 Points

try, expect, and else.

def add(five, eight): try: five = float(five) eight = float(eight) except: ValueError: return("None") else: return(five + eight)

I get pass until the last step... I don't know what's wrong with this step..

trial.py
def add(five, eight):
try:
    five = float(five)
    eight = float(eight)
except: ValueError:
    return("None")
else:
    return(five + eight)
Antonio De Rose
Antonio De Rose
20,884 Points

Your logic of thinking is right, and towards getting the end goal is correct, however there are 3 issues in your code.

#1) first the code, have not been indented, in python, the code need have to be indented, 
#so in this function, I do not see the indentation of the code
def add(five, eight):
try:
    five = float(five)
    eight = float(eight)
except: ValueError:#2) check this line, is this correct
    return("None")# 3) You are returning a string here, None is like null, it is the state of empty or no value
else:
    return(five + eight)

1 Answer

Antonio De Rose
Antonio De Rose
20,884 Points
def add(five, eight):
    try:
        five = float(five)
        eight = float(eight)
    except ValueError:
        return(None)
    else:
        return(five + eight)