"Python Basics (2015)" was retired on June 22, 2018. You are now viewing the recommended replacement.

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

Emilio Andere
Emilio Andere
495 Points

exception of the rule: try

why is this wrong

trial.py
def add(num1, num2):
    try:
        count = int(input("What is your age")
                    except ValueError:
                        return("None.")
                    else:
    a = float(num1)
    b = float(num2)
    return(a + b)

add(467, 789)

1 Answer

Elad Ohana
Elad Ohana
24,456 Points

Hi Emilio,

There are certain issues with your code: First, your indentation is off; your try, except, and else should all be on the same indent, for instance. You also added the count variable that asks the user for their age, and this part is not in the challenge. Finally, in your except statement, you are returning "None." which is a string, instead of the quasi-boolean value of None. Also, the end of your code calls the function, which doesn't affect your challenge success (at least with my tests), but is not called for either. Keep in mind that these challenges can be very specific and can fail for very minor things like that.

Try modifying your code a little bit and you should be able to get it!

Elad.

Greg Kaleka
Greg Kaleka
39,021 Points

Elad, great answer :blush:

Emilio - just to underscore one thing here: indentation in Python is not a minor thing! Unlike other languages that use braces to delineate code blocks, and are not dependent on "correct" spacing and indentation, Python uses indentation as the sole way to delineate code blocks. If you don't indent your Python code properly, things will break!