简体   繁体   中英

Python Print statement comes out blank output

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)

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

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

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)

As others have said, you need to call the function. At the end of your file just call

num1 = 2
num2 = 8

def add_numb(num1, num2):
   ...

add_numb(num1, num2)

It is important to note that the num1 and num2 where you assign them to ints

num1 = 2
num2 = 8

are not the same thing as the num1 and num2 in the line

def add_numb(num1, num2):
    ....

In the first part, you are creating two variables when you assign them values ( 2 and 8 ). In the second part, you are defining two function arguments named num1 and num2 . They can be named anything and are scoped to the function you are creating. Their value is taken from whatever you call the function with and using their name inside the function will always refer to those arguments. I feel like this is where the confusion stemmed from.

(Slightly more advanced)If you wanted to use the global variables inside the function, you'd need them to have different names as the parameters. You can even alter their values by declaring them with the keyword global , such as:

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

def add_numb(first, second):
    global num1
    global num2

    num1 = first
    num2 = second
    myNewSum = first + second
    print(myNewSum)

Hopefully this gave you a little background and wasn't overly confusing

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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM