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

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

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!