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 trialAndrew McMaster
1,377 PointsEnsuring arguments are integers
Having a problem understand task 3 of 3. I have to ensure the arguments are numbers. I think the line that is wrong is add = int().
Any pointers?
def add(num1, num2):
try:
add = int()
except ValueError:
return None
else:
return float(num1) + float(num2)
2 Answers
Derek Woods
6,272 PointsYou're pretty close, try, except and else need to all be at the same indentation level. Also I just put the return equation in the try section and didn't use the else: it still passes
Andrew McMaster
1,377 PointsMmmm. Still not able to get this to pass.. I have the following:
def add(num1, num2): try: add = int() except ValueError: return None else: return float(num1) + float(num2)
It now states the following error:
ValueError: could not convert string to float: 'a'
Derek Woods
6,272 Pointstry putting what is in else in the try. try: return float(num1) + float(num2) except ValueError: return none and leave out the else
Derek Woods
6,272 PointsDerek Woods
6,272 PointsJust to add on it will "try" to add the floats and return the answer and if there is a ValueError because you are trying to add a string or whatnot it will go to the "except ValueError:" section and return None