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

Mamadou Diene
seal-mask
.a{fill-rule:evenodd;}techdegree
Mamadou Diene
Python Web Development Techdegree Student 415 Points

You're doing great! Just one more task but it's a bigger one. Right now, we turn everything into a float.

i am stuck at this level, it keeps taking me back to task 1 because it is not passing anymore

trial.py
def add(bottles,boxes):
try:
    return(float(bottles) + float(boxes))
    except:valueError
           else:  
                return None

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

You're pretty close to the solution. Just some minor syntax changes... see below. Bear in mind that this challenge can be solved with a slightly simpler version of code, but the emphasis is good programming style.

def add(bottles, boxes):
    try:
        value = float(bottles) + float(boxes)
    except ValueError:
        value = None
    else:
        return value

You could also do a simpler version which also passes the challenge, but isn't quite as good in terms of programming style, since it has multiple return lines. Personally, I don't follow that rule if it makes code unnecessarily complicated.

def add(bottles, boxes):
    try:
        return float(bottles) + float(boxes)
    except ValueError:
        return None