为了账号安全,请及时绑定邮箱和手机立即绑定

使用的脚本定义python函数,但是却报错了,该怎么解决?

使用的脚本定义python函数,但是却报错了,该怎么解决?

开心每一天1111 2021-12-02 11:07:19
对照着书本的教导,定义了函数,[root@binzai learning-python]# cat sysinfo.py#!/usr/bin/env pythonimport subprocessdef diskspace():diskspace="df"diskspace_arg="-h"print "Gathering diskspace information with %s command:\n" % diskspacesubprocess.call([diskspace,diskspace_arg])diskspace()运行时报错了,小弟不解,python比bash shell麻烦多了[root@binzai learning-python]# ./sysinfo.pyFile "./sysinfo.py", line 4diskspace="df"^IndentationError: expected an indented block
查看完整描述

2 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

这个是说让你缩进的。
Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的Python程序员,也可能陷入陷阱当中。最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分别的。
在编译时会出现这样的错IndentationError:expected an indented block说明此处需要缩进,你只要在出现错误的那一行,按空格或Tab(但不能混用)键缩进就行。
往往有的人会疑问:我根本就没缩进怎么还是错,不对,该缩进的地方就要缩进,不缩进反而会出错,,比如:
if xxxxxx:

(空格)xxxxx

或者

def xxxxxx:

(空格)xxxxx

还有

for xxxxxx:

(空格)xxxxx

一句话 有冒号的下一行往往要缩进,该缩进就缩进



查看完整回答
反对 回复 2021-12-07
?
RISEBY

TA贡献1856条经验 获得超5个赞

#!/usr/bin/env pythonimport subprocessdef diskspace():    diskspace="df"    diskspace_arg="-h"    print ('Gathering diskspace information with %s command:\n'  %diskspace)    subprocess.call([diskspace,diskspace_arg]) diskspace()

查看完整回答
反对 回复 2021-12-07
  • 2 回答
  • 0 关注
  • 264 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信