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

Need help with Trial.py quiz in the Python Basic course.

I'm not able to figure out what I'm doing wrong with this code. I will appreciate any assistance. Thank you.

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

2 Answers

There are two problems.

  1. You are converting num1 and num2 into integers in the try block. The questions says to convert the two numbers into floats in the try block.
  2. If a ValueError occurs, the question says to return None. You literally returned nothing. What you need to do is put return None inside the except block.

Here is the code that allowed me to pass the challenge.

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

Hope this helps, and feel free to ask any questions.

Thank you, Aaron! I appreciate your help.

Your welcome!