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

I can't figure out what I am doing wrong here. please help.

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

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

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

My guess is that you're in part 2 of the challenge, no?

For part 2, you do not need to use while loop for the task. The way you use the float() isn't wrong, but it won't have effect on number 1 or 2's original values.

Here's how you should write it

def add(a,b):
    a = float(a)  # convert a to float
    b = float(b)  # convert b to float
    return a + b

Make sense?


And for part 3 of the challenge, you will have to use try...except...else clauses per the challenge instructions.

def add(a,b):
    try:
        a = float(a)  # convert a to float
        b = float(b)  # convert b to float
    except ValueError:
        return None
    else:
        return a + b
KaviArasu N S
KaviArasu N S
4,185 Points

I did all that right and still the program showing me 'error'. Figured it out, it was due to improper indentation! Indentation Matters!