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

exception values help !!!!!

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.

trial.py
def add(num1, num2):
    try:
        num1= input("Enter the first number")
        num2= input("Enter the second number")
        a= float(num1)
        b=float(num2)
        return(a + b)
    except ValueError:
        return(none)
    else:
        return(a + b)

total = add(2, 2)
print(total)

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Atul, just three small issues with your code.

1. There's no need to get input from the user, just remove this part.

2. Returning none is done by return None. The None must be capitalized.

3. There is an extra return statement, remove one of them. Either the one in the try block, or the else. Either one will work, and I cannot recommend one over the other.

def add(num1, num2):
    try:
        a = float(num1)
        b = float(num2)
    except ValueError:
        return None
    else:
        return a + b

Thank You Umesh

Can you provide a "Best Answer" please?

It gives a bonus 12+ forum points to the user who receives the best answer and it also provides an extra "Check Mark" in the community next to this question which tells everybody "This question is answered!". It could really help :)

Thank you for understanding. ~Alex