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

Nikhil Alexander
Nikhil Alexander
1,444 Points

task 2 is no longer passing after i wrote the third part of the task... please help me correct my error

after writing the third part of the task the second task is being expressed as invalid......... i am not able to find out the error pertaining to the third task... will be grateful for any help or advice given...

trial.py
def add(num1,num2):
    return(num1 + num2)

add(1,1)

def add(num1,num2):
    return(float(num1) + float(num2))

add(1,1)

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

add(1,1)

1 Answer

Hey Nikhil! Couple of things here. You shouldent make multiple add functions, you should modify the already existing one at each step.

Also, the backend will send numbers to the function, so you dont need to do it yourself.

Lastly, using the float() function, returns a float from what you pass into it, but it dosent actually change the value.

test = 5
float(5) # This would return 5.0
print(test) # Test would still just be 5 tho.
# So you need to do:
test = float(test) # Now test would be 5.0

A little hint would be to just return the result of your expression in the try:

return float(num1) + float(num2)

And then just remove that else statement. Hope this helps!

Nikhil Alexander
Nikhil Alexander
1,444 Points

really appreciate the explanative approach....to help me... but i am still not able to pass the code in that manner... could u show me your piece of code... that would really help.. sorry for this trouble

Hey again Nikhil! I will just copy your code as it was good:

def add(num1,num2):
    try:
        (float(num1) + float(num2)) # This line of code is your problem.
    except ValueError:
        return(None)
    else:
        return(num1 + num2)

At that line, your testing if you can convert num1 and num2 to flaots. But as i pointed out, it will not actually change the value of num1 and num2.

So say they can be converted to floats, you will go right down to that else statement and return num1 + num2 as non floats.

You could fix this in multiple ways. In your else statement you could just do:

else:
    return float(num1) + float(num2)

But you could also just do this in the try statement directly:

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

Hope this helps!

Nikhil Alexander
Nikhil Alexander
1,444 Points

thank you for the time and explanative approach you provided......it was a really a great help to me.. i was able to pass the last challenge of the track because of your help... thank you once again!!!!!!!

Glad that i could help! You can mark the question as "solved" by sellecting a "best answer".