),python-3.x,linux,debian-buster"/>

首页 > 解决方案 > 在终端 debian 中运行 python 脚本(file.py),错误()

问题描述

我在终端中运行 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$ 

标签: python-3.xlinuxdebian-buster

解决方案


您正在打印函数对象。如果你想执行一个函数,你必须在函数名后使用括号。由于您的函数没有返回值,因此“打印”也会在输出中显示“无”值。因此在这种情况下使用 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

推荐阅读