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

Siyuan Welch
Siyuan Welch
3,025 Points

Add a try block before where you turn your arguments into floats. Then add an except to catch the possible ValueError.

Add a try block before where you turn your arguments into floats. Then add an except to catch the possible ValueError. Inside the except block, return None. If you're following the structure from the videos, add an else: for your final return of the added floats.

Siyuan Welch
Siyuan Welch
3,025 Points

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

7 Answers

Hey Siyuan! I tried your code and it worked. Probably you have to indent smth. Could you write your code with synthax highlighting,please?

Siyuan Welch
Siyuan Welch
3,025 Points

Thank you so much Deni, Oh, I see. You are right. It's the indent missed, it passed right now. And actually, I have no idea how to write code with synthax highlightning in community. I would like figure it out.

Hey Siyuan! I was stuck too, till I realized that its an issue with the indentation and not your code. "Def ....." line has no indentation, the rest of your lines should have indentation & I believe you will get through!

Jeffery Austin
Jeffery Austin
8,128 Points

You almost got it you just need to capitalize None, and you don't need parentheses for return.

return float(num1)+float(num2)

Hello Siyuan! I think you should write None (from capital letter). python is case-sensitive. Good luck!

Always welcome! to highlight your syntax just put 3 backticks (```) and specify the language. Good luck!

Siyuan Welch
Siyuan Welch
3,025 Points

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

Siyuan Welch
Siyuan Welch
3,025 Points

I corrected them but still can not pass. Please help me out!

Ilia Galperin
Ilia Galperin
6,522 Points

def add(arg1,arg2): try: sum = float(arg1)+float(arg2) except: return None else: return sum