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 trial

Python Python Basics (2015) Logic in Python Try and Except

Joseph Duarte
Joseph Duarte
468 Points

cannot 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

Andrew Winkler
Andrew Winkler
37,739 Points

Well for starters, your code is improperly formatted. Follow this syntax instead:

def add(num1, num2):
    return float(num1) + float(num2)

2 Answers

Sage Elliott
Sage Elliott
30,003 Points

Short 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
Joseph Duarte
Joseph Duarte
468 Points

I actually got it using this syntax. Thx for the answer.

Sage Elliott
Sage Elliott
30,003 Points

You 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
Lorenzo Minto
13,898 Points

I'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)