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

Help - def Challenge

Hi,

Need your help with the next question:

Q: Right now, we turn everything into a float. That's great so long as we're getting numbers or numbers as a string. We should handle cases where we get a non-number, though. Add a try block before where you turn your arguments into floats. Then add an except to catch the possible ValueError. Inside the except block, return None. If you're following the structure from the videos, add an else: for your final return of the added floats.

I tried:

def add (a, b):
    try:
    print(a * b)
    except ValueError:
        return(None)
    else:
    f = float(a)
    g = float(b)
    return (f + g )

I did not understand what I did wrong.

4 Answers

Hi Omri ,

Before I answer, is your formatting exactly as is shown on the screenshot?

Steven Parker
Steven Parker
243,656 Points

:point_right: You appear to have some indentation and line-order issues.

Any line that is part of the try should be indented. So should the lines that are part of the else, but instead you can just remove the else since it is not needed (because the except returns).

Also, based on the description of the challenge the lines that perform the float conversion should be moved up and included in the indented try block. And it doesn't sound like that print line should be there at all.

In future, be sure to include a link to the challenge page.

Hi, I am having trouble with this code challenge could someone help me with it also: https://teamtreehouse.com/library/python-basics/logic-in-python/try-and-except

Here's what I put:

try: def add float(one, two):
hello = float(one) world = float(two) except ValueError:
return None else:
return (hello + world)

Hi Kaisha,

Firstly you may have a problem with your function name, in the previous exercises it was called add
so I would leave it at that. It may be how it appears in the browser but you have no '_' in your function name.

i.e it should be add_float.

Most importantly though your try block is actually outside your function rather than inside. All try, except, else blocks
should be indented as below.

Also just assign your two arguments to one variable( in my case tot = float(one) + float(two), there is no need
to assign it to two.

Please see screenshot below for my example.

def add(one, two):
        try:
            tot = float(one) + float(two)
        except ValueError:
            return None
        else:
            return tot