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 trialJoseph Duarte
468 Pointscannot pass this task: Challenge Task 2 of 3
If I do the following in Python IDE and pass numerical valus this works, s I am not sure what I am doing wrong:
def add(num1, num2): return(float(num1 + num2))
Need help passing this challenge
2 Answers
Sage Elliott
30,003 PointsShort answer: I know you can pass the quiz by converting the numbers passed into floats separately.
def add(x , y):
a = float(x)
b = float(y)
return a + b
Andrew Winkler
37,739 PointsSage Elliott - that seems a bit wet ;P
Joseph Duarte
468 PointsI actually got it using this syntax. Thx for the answer.
Sage Elliott
30,003 PointsYou can use any syntax that you find the best. Mine may not be the most pythonic or efficient, but I personally like doing conversions before the return :)
Lorenzo Minto
13,898 PointsI'm not really sure what's going on under the hood but it seems that num1 and num2 in your code are considered to be strings and the plus signs becomes a concatenation operand between the two given strings. This means that when you use float on the sum you're trying to convert a number like 25.5.5 to float which of course makes no sense at all. This code should work:
def add(num1, num2):
return float(num1) + float(num2)
Andrew Winkler
37,739 PointsAndrew Winkler
37,739 PointsWell for starters, your code is improperly formatted. Follow this syntax instead: