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 trialMoses Lagoon
Courses Plus Student 660 PointsDoing this for try catch of the exceptions and it's not working.
def add(x,y): try: nx = float(x) ny = float(y) except valueError: return None else: return(nx + ny)
def add(x, y):
try:
nx = float(x)
ny = float(y)
except ValueError:
return None
else:
return (nx + ny)
2 Answers
Jeremy Hill
29,567 PointsThere is multiple ways that you can write this and I'm almost 100 percent sure someone will gig me for not showing you a quicker way to write this lol but I think this is a better way to show beginners as it seems a little easier to comprehend what is going on, with all that said here is the code:
def add(arg1, arg2):
try:
arg_float1 = float(arg1)
arg_float2 = float(arg2)
except ValueError:
return None
else:
return arg_float1 + arg_float2
Moses Lagoon
Courses Plus Student 660 Pointslol. I figured. Thanks.
Jeremy Hill
29,567 PointsJeremy Hill
29,567 PointsAs I now look at your code closely you just need to remove your parenthesis lol.