0

I apologize if this seems very basic, I'm new to Python and learning. I have a task I'm working on but I can't seem to get the print to work in my function. When I run it, I get nothing, no output. I've tried looking up the basics for this, but maybe it's so simple I'm overlooking it? Any guidance would be appreciated.

# create a function which receives two integers as input, adds them. Run your function with integers 2 and 8, and save the output to a new variable called myNewSum. Print myNewSum. expected outcome: 10

num1 = int(2)
num2 = int(8)

def add_numb(num1, num2):
    myNewSum = num1 + num2
    print(myNewSum)
3
  • are you calling add_numb? Commented Oct 22, 2019 at 16:08
  • BTW, you don't need num1 = int(2). 2 is an integer, so num1 = 2 is enough. Commented Oct 22, 2019 at 16:09
  • 1
    Another side note, depending on the intention, and pickiness, of this problem you may want to read the statement more closely, do they want you to print the value returned by the function or print the value in the function? Commented Oct 22, 2019 at 16:20

4 Answers 4

2

Call your function:

add_numb(2, 8)

EDIT ( Since you're learning ):

num = int(2) # int() is redundant here as Python already knows that 2 is an int
Sign up to request clarification or add additional context in comments.

1 Comment

Your method call doesn't match your definition, add_numb vs add_num
0

You did all right, just forget to call function. You just defined function, start to use it. You can read more about in official documentation.

add_numb(num1, num2)

Output:

10

Also, as @Matthias told in comments, you don't need to use int():

num1 = 2
num2 = 8
# lets check type of variable:
type(num1)

int

Comments

0

If you include a main function to your python program it makes it more reusable and I think easier to understand whats going on. For more information on the main follow this link.

I changed the global variable num1 and num2 from your example to var1 and var2, to emphasize the difference between global variables and parameters of a function.

var1 = 2
var2 = 8

def add_numb(num1, num2):
    myNewSum = num1 + num2
    print(myNewSum)

if __name__== "__main__":
    add_numb(var1, var2)

Comments

0

You had to call the method.

num1 = int(2)
num2 = int(8)

def add_numb(num1, num2):
        myNewSum = num1 + num2
        print(myNewSum)

add_numb(num1, num2)  #calling the method  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.