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 trialCarole Cox
429 PointsHmmm..where am I going wrong?
I passed the first challenge with this code but when I put float() into the code, it started to fail. I tried having the arguments as float (num1, num2) but the challenge did not like that either. I appreciate any suggestion!
def add(num1,num2):
float(num1)
float(num2)
add=num1+num2
return(add)
7 Answers
jcorum
71,830 PointsSorry, I missed your question about "try and except". Yes, for the next part:
def add(num1, num2):
try:
num1 = float(num1)
num2 = float(num2)
except ValueError:
return None
else:
return num1 + num2
jcorum
71,830 PointsCarole, the problem is that float(num1) needs to be saved back to num1, etc for num2:
def add(num1, num2):
num1 = float(num1)
num2 = float(num2)
return num1 + num2
The way you have it your float conversion is wasted. As you go on to use the unconverted num1 and num2.
Carole Cox
429 PointsWOW -newbie mistake! Thanks!
I am wet behind the ears!
jcorum
71,830 PointsNo, it happens to us all!!
Carole Cox
429 PointsThanks! I need all the encouragement I can get.
Carole Cox
429 PointsHa Ha! how did you know I was fighting with that. I'm close! this is a BIG HELP!
Carole Cox
429 PointsHa Ha! how did you know I was fighting with that. I'm close! this is a BIG HELP!
Carole Cox
429 PointsCarole Cox
429 PointsDo I need a Try and except?