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

Fernandito Flores
seal-mask
.a{fill-rule:evenodd;}techdegree
Fernandito Flores
Python Web Development Techdegree Student 916 Points

Project 1 challenge 3 exceptions

Hello. I really don't know what question to ask. It seems like the code is correct. I ran the code on IDLE and it worked.

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

I had to add the function call and print to return total in IDLE. Not needed in challenge.

print(add(10,6)) #returns 16 print(add('five',6) #returns None

On the challenge, I get bummer! try again.

Any pointers will be greatly appreciated.

trial.py
def add(num1,num2):
try:    
    total = float(num1) + float(num2)
except ValueError:
    return None
else:
    return total

2 Answers

Derek Woods
Derek Woods
6,272 Points

i tried your code and everything worked, all i had to do was indent everything that is part of the function. Python is big on formatting whitespace.

Fernandito Flores
seal-mask
.a{fill-rule:evenodd;}techdegree
Fernandito Flores
Python Web Development Techdegree Student 916 Points

I see. I had to indent try/except/else as well as the blocks associated with it. It worked! Thank you so much. I been trying to figure this out since yesterday.