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!

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

I'm able to get this code to work in Terminal but not in the workspace. Why am I seeing a value error in the workspace?

my function adds two numbers and stores it into a variable called "total" which converts the (int) arguments into floats.

When I call the function, I pass it 2 integer parameters. It works when I run the code in terminal but I'm not sure why I'm getting the ValueError.

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

mynum = add(3,4)

Ok. So I modified the code to do this and it worked.

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

I just want to mention something: A LOT of stuff can "work" in the Terminal or your IDE...A LOT But in these challanges on treehouse it does not matter if you can just compile your code. The code has to return the values the instructor wanted AND you have to use the methods that are required.

Just sayin ;) always read what answer is exactly required and how you should create the solution... Most mistakes ins this forum are not by people who dont understand how to code the solution...most of the time they just dont read carefully enough...or they miss something.

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 68,390 Points

The issue is when the arguments are both strings. If the concatenation occurs before converting to float(), say, "2.5" and "5.2", then float() is run on "2.55.2" which raises the error. Instead use float() on each argument:

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