0

I am running a python script (file.py) in terminal and it doesn't print the results of the function. Below you can find the a print of my terminal, thanks.

stest@debian:~/Documents/python$ ls -l | grep tic_tac.py 
-rwxr-xr-x  1 stest stest  270 Oct 14 15:58 tic_tac.py
stest@debian:~/Documents/python$ cat tic_tac.py 

    #!/usr/bin/env python
    d_b = ["  " for i in range(9)]
    print (d_b)
    def b():
        r_1 = "|{}|{}|{}|".format(d_b[0],d_b[1],d_b[2])
        r_2 = "|{}|{}|{}|".format(d_b[3],d_b[4],d_b[5])
        r_3 = "|{}|{}|{}|".format(d_b[6],d_b[7],d_b[8])
        print (r_1)
        print (r_2)
        print (r_3)
    print (b)

stest@debian:~/Documents/python$ ./tic_tac.py 

    ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
    <function b at 0x7f6bd9f28668>

stest@debian:~/Documents/python$ python3 tic_tac.py 

    ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
    <function b at 0x7f63232c01e0>

stest@debian:~/Documents/python$ 
sty
  • 1
  • Thank you guys, I see now how inexperienced I am, your answers were very helpful. – sty Oct 15 '19 at 09:54

2 Answers2

1

You're printing the function object. If you want to execute a function, you have to use parentheses after the function name. Since your function has not a return value, 'print' will also displays a 'None' value on the output. So using print in this case is redundant and you only need to call the function right away.

def b():   # Your function definition
    ...

b()    # Function call (The correct way)
print(b())    # Executes the function but also prints 'None'
print(b)    # Only prints the function object without executing it
Saeed Ahadian
  • 196
  • 1
  • 6
  • 13
0
d_b = ["  " for i in range(9)]
print (d_b)
def b():
    r_1 = "|{}|{}|{}|".format(d_b[0],d_b[1],d_b[2])
    r_2 = "|{}|{}|{}|".format(d_b[3],d_b[4],d_b[5])
    r_3 = "|{}|{}|{}|".format(d_b[6],d_b[7],d_b[8])
    print (r_1)
    print (r_2)
    print (r_3)
b()  # Don't write this "print (b())" it will print "None"
Lucifer
  • 282
  • 3
  • 13