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

Carole Cox
Carole Cox
429 Points

Hmmm..where am I going wrong?

I passed the first challenge with this code but when I put float() into the code, it started to fail. I tried having the arguments as float (num1, num2) but the challenge did not like that either. I appreciate any suggestion!

trial.py
def add(num1,num2):
  float(num1)
  float(num2)
  add=num1+num2
  return(add)
Carole Cox
Carole Cox
429 Points

Do I need a Try and except?

7 Answers

Sorry, I missed your question about "try and except". Yes, for the next part:

def add(num1, num2):
  try:
    num1 = float(num1)
    num2 = float(num2)
  except ValueError:
    return None
  else:
    return num1 + num2

Carole, the problem is that float(num1) needs to be saved back to num1, etc for num2:

def add(num1, num2):

    num1 = float(num1)
    num2 = float(num2)

    return num1 + num2

The way you have it your float conversion is wasted. As you go on to use the unconverted num1 and num2.

Carole Cox
Carole Cox
429 Points

WOW -newbie mistake! Thanks!

I am wet behind the ears!

No, it happens to us all!!

Carole Cox
Carole Cox
429 Points

Thanks! I need all the encouragement I can get.

Carole Cox
Carole Cox
429 Points

Ha Ha! how did you know I was fighting with that. I'm close! this is a BIG HELP!

Carole Cox
Carole Cox
429 Points

Ha Ha! how did you know I was fighting with that. I'm close! this is a BIG HELP!