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

stephan de grove
PLUS
stephan de grove
Courses Plus Student 564 Points

Hi, i may have understand something wrong. Seems to me thats should working fine

May have miss understood the question

trial.py
def add(one,two):
    return one + two

try:
    one = float(input("Please enter an"))
    two = float(input("Please enter a second an"))
except ValueError: 
     None
else:
  add(one,two)

2 Answers

AJ Salmon
AJ Salmon
5,675 Points

Hello Stephan,

Firstly, your code needs to all be in the add function. This means it should all be indented, like this:

def add(one, two):
    #code
    #will
    #go
    #here

That way, all of your code will be contained in the function, so when the function is called, it will all be run.

Secondly, the challenge wants you to try to convert the arguments to add into floats, add them together, and then return the total. If the numbers can't be converted to floats, it will raise a ValueError. Writing an except ValueError block will catch this. In that case, you are supposed to return None, like this:

except ValueError:
    return None

Again, this will all be contained inside the add function. Here's something to get you started:

def add(one, two):
    try:
        total = float(one) + float(two)
    except ValueError:

Now, you finish the code. In the except block, you'll need to tell it to return None if there's a ValueError. Otherwise, in an else block, return total. I know you can do it! Happy coding :)

stephan de grove
PLUS
stephan de grove
Courses Plus Student 564 Points

Thanks a lot AJ as i said i should have misunderstood the problem. Much clearer with you. Best regard and Thanks a lot for your help.

AJ Salmon
AJ Salmon
5,675 Points

I'm glad I was able to help make it more clear! It's my pleasure :)