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

Greg Isaacson
PLUS
Greg Isaacson
Courses Plus Student 702 Points

Please help me with this code!!

Hi guys!:)

I need some help.

The question is asking me to make a function that will add A and B together.

As I understand it:

First define my function: def add_function(add): -----The () is the place holder for the arguments the function needs to be?? So either add or (a,b)?? I am not sure exactly.

Then I type:

if True (I am using the if true to trigger the code automatically). a+b (That is what I want to do).

Then I type Return to return the result.

Then I call?? the function? I know I need to type function() on all functions...but I am unsure what needs to go in the parenthesis.

Thanks for any help!!!

trial.py
def add_function(add):
    if True:
        a+b
    return 
add_function(add)

2 Answers

Robert Lyon
Robert Lyon
7,551 Points

In this challenge you are being asked to create a function called add that takes two arguments and return the sum of the arguments

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

as you can see about I have declared the function add. my two arguments num1 and num2 are contained within the brackets. the return statement takes both of the arguments, adds them together and returns the result.

to print the output of this function i would use the print command and call the function as follows.

print(add(2, 3))

as you can see above I am calling the function with the parameters 2 and 3. These values then get passed to the function through the arguments num1 and num2 respectively. the return statement returns the result which then gets sent to the print function. Which then in turn prints to the screen.

I hope this helps you out. You may want to have a look at a lesson on parameters and arguments.

Greg Isaacson
PLUS
Greg Isaacson
Courses Plus Student 702 Points

You explained perfectly. I understand now.

Thanks!!

Robert Lyon
Robert Lyon
7,551 Points

Your welcome. happy to help