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 Technical Interview Prep: Python Basics Basic Python How Do You Like Them Apples

Technical Interview Prep: Python Basics > How Do You Like Them Apples

Bummer: Uh oh, I didn't get the correct value returned. It should have been 59. Instead, it was 18

mathy.py
#Create a function called "mathy"
# that calls a string and two numbers
def mathy(str, num1, num2):

    #Add two numbers and return the sum
    return num1 + num2

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hey Trenton Spears, the challenge will call your function with string equal to the one of the four operator names. You will need to check the value of string to decide the math operator to use. An if/elif/else statement works well here.

Also, str is a built-in object type. Using str as a variable name should be avoided if possible.

Post back if you need more help. Good luck!!!

Hey, Chris Freeman thanks for reaching out, unfortunately, I'm still stuck. Is there either another way you could break down your guidance or, a video section within a particular course you maybe could recommend?

There's gotta be something wrong with my if/elif/else statement, am I right?..

Bummer: Uh oh, I didn't get the correct value returned. It should have been 50. Instead, it was 150

def mathy(sum, x, y):
    sum = x + y

    if sum > x + y:
        return False

    elif sum < x + y:
        return False

    else:
        return sum
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Your answer should be in the form

if sum == β€œadd”:
    return num1 + num2
elif sum == β€œmultiply”:
    # you’ve can do the rest

Is there a specific lesson video or section on the platform you can point me to so that I can refresh myself on the exercise requirements?

Bummer: Uh oh, I didn't get the correct value returned. It should have been 50. Instead, it was 150

def mathy(x, num1, num2):
    x = num1 + num2

    if x == "add":
        return num1 + num2
    elif x == "multiply":
        return num1 * num2

    else:
        return x
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

The line x = num1 + num2 overwrites the parameter x. Move this line to the else block. Better yet, there isn’t a need to assign to x. You can simply return num1 + num2

Be sure to include all four operating names.

Got it!

def mathy(x, num1, num2):

    if x == "add":
        return num1 + num2
    elif x == "subtract":
        return num1 - num2
    elif x == "multiply":
        return num1 * num2
    elif x == "divide":
        return num1 / num2
    else:
        return x

Thanks again Chris