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

Sterling Valentine
Sterling Valentine
439 Points

How do I confirm a number is a number using a "try:" block?

I've tried a few different Boolean checks on trial.py, but every time I put a statement in the try block that seems like it should work, it tells me that the portion of the task where I convert the numbers into floats is no longer passing.

trial.py
def add(num1, num2):
    try:
        num1 == (int(num1) or float(num1)) and (int(num2) or float(num2))
    except ValueError:
        return None
    else:
        num1float = float(num1)
        num2float = float(num2)
        return(num1float + num2float)

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi! Just two days ago I posted what I feel is a rather decent explanation of this particular challenge. You can find the solution and explanation here: https://teamtreehouse.com/community/hi-learning-about-python-in-python-basics-stuck-on-this-lesson

Hope this helps!