我正在终端中运行一个python脚本(file.py),它不会打印函数的结果。在下面你可以找到我的终端打印,谢谢。

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$

最佳答案

您正在打印函数对象。如果要执行函数,必须在函数名后使用括号。由于函数没有返回值,“print”还会在输出上显示“None”值。因此在这种情况下使用print是多余的,您只需要立即调用函数。

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

关于python-3.x - 在终端debian中运行python脚本(file.py),错误(<功能b在0x7f63232c01e0>),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58377960/

10-10 21:57
查看更多