Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Logic in Python Try and Except

Is the issue with my "Try" block the number of arguments represented?

I don't see where its syntactically necessary to have multiple arguments in the input function under my try block, but I'm thinking that such maybe the issue as to why my code is incorrect. Could someone skim it for me and tell me the issue please?

-Thanks

trial.py
def add(num1,num2):
  try:
      addition= int(input("Give me a number: ")
  except ValueError:
      return("None")
  else:
       num1=float(num1)
       num2=float(num2)
       return(num1+num2)

1 Answer

Haider Ali
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Haider Ali
Python Development Techdegree Graduate 24,724 Points

Hi there Reign, there are just a couple of things wrong with your code. Firstly, in the try block, you should be attempting to convert the 2 arguments to floats using the float() function. Also, return is a keyword, not a function so it does not require parenthesis after it. Furthermore, None is not a string, so it does not require quote marks around it. Make these changes and you should be good to go!

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

If you have any further questions please let me know :).

Thanks,

Haider

THANK YOU SO MUCH!!!! Many of the modifications you've stated I actually have tried. But because ,at any given point in my attempts, I never made the proper mods all together at once. but again thank you!