我在终端中运行一个 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”也会在输出中显示“无”值。所以在这种情况下使用 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),错误 (<function b at 0x7f63232c01e0>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58377960/