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 trialjammie Hargreaves
Courses Plus Student 2,455 Pointstrial.py
Can someone please assist me ? Not sure what im doing wrong.
def add(x,y):
try:
return float(x) + float(y)
except ValueError:
return None
else:
return float(x) + float(y)
1 Answer
james south
Front End Web Development Techdegree Graduate 33,271 Pointsindentation matters in python and inside the function it needs to look like this:
try:
body
except:
body
else:
body
what makes each part called body a part of the respective block it is in is being indented. the except is different from the try part because it is at the same level of indentation. next you must understand that if what happens in the try works, the else will execute, so you don't need to do the same thing in both. if the try fails, the except executes. so in the else, you can just return the sum of the numbers. in the try, all you do is cast them to floats.