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

Emilio Andere
Emilio Andere
495 Points

why wrong

why is this wrong

trial.py
    add(467, 789)
def add(num1, num2):
    try:
       add = int(input("Sum of these two numbers?"))
   except ValueError:
        return(None)
    else:
        a = float(num1)
        b = float(num2)
         return(a + b)

2 Answers

Steven Parker
Steven Parker
229,657 Points

Here's a few hints:

  • you only need to define the function for the challenge, you won't need to call it yourself
  • the float conversions must be done inside the try section to catch the exceptions
  • the arguments are passed directly to the function, so no input will be needed
Emilio Andere
Emilio Andere
495 Points

def add(num1, num2): try: a = float(num1) b = float(num2) add = int(input(num1 + num2)) except ValueError: return(None) else: return(a + b) ??????????

Emilio Andere
Emilio Andere
495 Points

so what do I put inside add if It is not input?

Steven Parker
Steven Parker
229,657 Points

I'm not sure what you intended to use the variable named "add" for, it doesn't seem to be related to the challenge. If you eliminate that line completely it looks like the new code might pass the challenge (I can't be sure because it is shown unformatted).

Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

And would the person who down-voted my answer please explain why?