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!

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

Oh man! Everything seems so different on these tasks than on the video.

I swear I am getting down everything the instructor is saying, but whenever I take on these tasks, it feels like I'm doing something completely different than anything related at all to the video I watched before. It's also different than what I did in Workspaces. It's getting really confusing and I'm losing confidence when I can't get over the Basic Python courses and I still have much harder courses ahead of me.

    a = float(1)
    b = float(2)
except ValueError:

2 Answers

K.D. Harris
K.D. Harris
11,223 Points

it's ok, don't feel discouraged! you'll get it :)

def add(arg1, arg2):
    try: #this block converts both arguments to floats before adding them together
        arg1 = float(arg1)
        arg2 = float(arg2)
    except ValueError: #...unless there is a valueerror indicating that the arguments are not integers
        return None
    else: #if there is no valueerror/the arguments are integers, then the above try block worked and you can add
        total = arg1 + arg2
    return total

I got it! Jeez! This to me was quite confusing and I am still questioning myself as to how I got it right. I sure hope I'll have a better understanding of this as I progress. The basics have been really hard and I'm scared of the courses that are more advanced I'll have to get through.

try: a = float(1) b = float(2)

here you are converting the integers to float.

try converting your function variables to float.

try: a = float(a) b = float(a)

then challenge will pass.