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

JAMES CADMAN
JAMES CADMAN
3,312 Points

TypeError: Can't convert 'int' object to str implicitly

I don't see anywhere in my code where an int is being converted to a str. I need some helping determining where the problem is.

trial.py
def add(a, b):
  total = a + b
  try:
    af = float(a)
    bf = float(b)
    total = af + bf
  except ValueError:
    return None
  else:
    return total

2 Answers

Steven Parker
Steven Parker
231,269 Points

I don't think the error message is pertinent. But you do have some extraneous code.

:point_right: just remove the "total = a + b" line.

Also, you don't really need an else since in the other case the function would have already returned. This doesn't affect the outcome (or the challenge), though.

JAMES CADMAN
JAMES CADMAN
3,312 Points

I removed that, and it fixed it. The code challenges are frustrating me because I can never tell if I am getting the code wrong, or taking something out from the last step that is making it say "Oops, step one is no longer correct" or something to that effect.

Thanks!

It helps to take your script into Workspaces if you feel like debugging may take some time and effort. You can edit quicker and get much more verbose feedback that way. Learning how to use pdb.set_trace() is a SUPER help to debugging your python code. It is pretty easy to use and takes very little time to learn. A set_trace() can walk you through your code, line by line, action by action and reveal values and variables at specific steps in the code. It saves me a lot of headache.