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 trialvikas pal
11,563 Pointsneed help
this is my code please tell me where i do mistake
def add(x,y)
try:
sum=return(float(x+y))
except ValueError:
return None
else:
return sum
add(5,5)
2 Answers
Jeff Hartl
Python Web Development Techdegree Student 8,420 Points- When you define a function there should be a colon.
-
sum=return(float(x+y)
is incorrect. Should there really be a return statement on that line, since later in your code you are returning sum? -
Else
statements cannot run without anif
statement higher up in the block of code. Since you are using a try\except block, think about whether you really need anelse
statement. - Could the function throw more exceptions than just a
ValueError
?
Wairton Rebouças
8,225 PointsJust one minor correction to Jeff Harti's answer. In python it is possible to use and else statement for exception handling, see section 8.3 of https://docs.python.org/3/tutorial/errors.html
The challenge asks the user to use an else statement for a try except block so, a valid answer would be:
def add(x,y):
try:
total = float(x)+ float(y)
except ValueError:
return None
else:
return total
note that the challenge asks you to convert to float before the sum
vikas pal
11,563 Pointsvikas pal
11,563 Pointsthanks to sort out my doubt