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

ravi thotapalli
PLUS
ravi thotapalli
Courses Plus Student 1,113 Points

What am I doing wrong here.

try: n1=input("Enter the first value ") n2=input("Enter the second value ") except ValueError: print(junk) else:
a=float(n1) b=float(n2) x=a+b print(x)

trial.py
try:
    def add(n1,n2):

        except ValueError:
            return()
else:     
    a=float(n1)
    b=float(n2)
    x=a+b
print(x)
return(x)
add(5,2)

1 Answer

Hi First time posting and maybe someone can explain better. I happened to see this and thought i'd try. It looks like you haven't placed your code inside the function and some layout needs a bit of tweaking, otherwise looks like you have everything.

I have tried to break it down below and I hope this helps a bit :) Keep it up :)

def add(arg1, arg2):    # creating a function to work within | tab in anything following
    try:    # the tabs need to be correct to be inside the function
        answer = float(arg1) + float(arg2)    # doing the maths using the arguments, answer is a new variable
        return answer   # If var answer is okay then it will return var answer
    except ValueError:   # catch the error | tabbing is important to line up
        return None # If an error found with var answer then return None