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

frank sebring
frank sebring
1,037 Points

I can't figure out why this answer is not correct. It runs in my complier and produces the correct results.

try: num1 = float(input("Enter a number ")) num2 = float(input("Enter a number ")) def add(num1, num2): return(num1 + num2) except ValueError: print("You did not enter a number!!") else: total = add(num1,num2) print(total)

trial.py
try:
  num1 = float(input("Enter a number "))
  num2 = float(input("Enter a number "))
  def add(num1, num2):
    return(num1 + num2)
except ValueError:
  print("You did not enter a number!!")
else:
  total = add(num1,num2)
  print(total)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Challenges going forward will use input() less and instead will call the functions you define directly in the grading process.

Also results of functions are not printed. Instead the are returned from the function using the return keyword.

So only the definition if add() is needed. Rearranging your code:

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

Will pass the challenge.