python-3.x - 在终端 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$
解决方案
您正在打印函数对象。如果你想执行一个函数,你必须在函数名后使用括号。由于您的函数没有返回值,因此“打印”也会在输出中显示“无”值。因此在这种情况下使用 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
推荐阅读
- php - 我想在 Laravel 中更新图像
- android - 我的 Firebase AdsMob 在运行时没有出现
- makefile - 警告:modules_install:缺少“System.map”文件。跳过 depmod
- mysql - 使用 Intersect 和电子邮件格式的多个输入值时获取 ER_PARSE_ERROR
- sql-server - 如何获取包含 where 子句中提到的每个项目的记录
- python - 如何从谷歌视觉文本检测 API 获得批量响应?
- reactjs - 如何仅在 Semantic-UI React 中为 Form.Dropdown 显示警告消息?
- amazon-web-services - Terraform 云初始化 AWS
- oracle - 在 Oracle SQL Developer 中显示调试输出
- javascript - jQuery Datatable 按钮展开/折叠所有