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

Arnav Goyal
Arnav Goyal
1,634 Points

Help in coding practice in python basics course.

I need help in challenge task 2 of 3 in the coding test.

trial.py
def add(num1, num2):
    return (num1+num2)
add(3, 4)

2 Answers

So this code works for the first task, but on task 2, you have to transform the a and b to float. Here is a better example:

def add(a, b):
  return (a + b)

Also, don't call the function, just write the function out. The Code Challenge will test the function for you.

For task 3, it is asking for some error handling. because if the value (in this case, a and b) transforming into a float might return an error, and that isn' t user-friendly. So you want to "catch" the error and just return None. Here is the full code:

def add(a, b):
  try:
    return float(a) + float(b)
  except ValueError:
    return None 

Hope it helps! ~xela888