Python 缩进混淆,使用4个空格的标签
我有一个小脚本:
filters = []
pipes = []
check_environment()
config()
fix_syslog()
make_fifo()
def check_environment():
# check python
# check for syslog
# check for mknod
# check for root privileges
def config():
accepted_filters = ['auth', 'authpriv', 'daemon', 'cron', 'ftp', 'lpr', \
'kern', 'mail', 'news', 'syslog', 'user', 'uucp', 'local0', 'local1' \
'local2', 'local3', 'local4', 'local5', 'local6', 'local7']
accepted_priorities = ['Emergency', 'Alert', 'Critical', 'Error', \
'Warning', 'Notice', 'Info', 'Debug']
print "Entered configuration mode. Type 'help' for more information"
loop = true
while loop:
command = input(">> ")
# handle the "done" command
if command == "done":
accept = input("Are you sure you are done configuring? (yes/no) ")
if accept == "yes":
loop = false
# handle the "help" command
elif command == "help":
print "help Displays this help message"
print "new Adds a filter to the temporary list"
print "show List all current temporary filters"
print "del Remove a filter from the temporary list"
print "done Commits to the filters and continues with install"
# handles the "show" command
elif command == "show":
for x in filters:
for y in pipes:
print filters.index(x), x, y
# handles the "new" command
elif command == "new":
new_filter = input("Enter desired facility/priority (eg. kern.Error): ")
separator = new_filter.index('.')
if separator == -1:
print "You've entered an invalid facility/priority. Please try again."
return
facility = new_filter[:separator]
priority = new_filter[separator:]
if facility in accepted_filters and priority in accepted_priorities:
filters.append(new_filter)
else:
print "You've entered an invalid facility/priority. Please try again."
return
new_pipe = input("Enter desired target pipe (kernel_error_pipe): ")
if new_pipe[0] != "|":
new_pipe = "|" + new_pipe
pipes.append(new_pipe)
# handles the "del" command
elif command == "del":
print "Run 'show' to see which filters are available to delete."
which_filter = input("Insert the number of the filter to delete: ")
filters.remove(filters.index(which_filter))
pipes.remove(pipes.index(which_filter))
# all other cases
else:
print "Invalid command. Type 'help' to see a list of available commands"
def fix_syslog():
# check over variables
# backup to specified folder
# write own file with comments
def make_fifo():
# create pipe folder
# create pipes
这可能是个糟糕的代码,但我刚开始调试,这是我第一次接触Python。我遇到了这个错误:
File "./install.py", line 31
def config():
^
IndentationError: expected an indented block
看起来所有的缩进都没问题,而且我已经设置了kate,让制表符等于4个空格。为什么会出现这个错误呢?有没有什么建议可以避免将来再出现这种情况?
3 个回答
2
这里缺少一个代码块:
def check_environment():
把它加上
def check_environment():
pass
而且你会遇到更多错误,因为你在定义之前就调用了 check_environment()
(和 config()
)。
3
在def check_environment():
这个地方,你需要写一些代码。如果你现在不想让它做任何事情,可以用pass
来表示。
def check_environment():
# check python
# check for syslog
# check for mknod
# check for root privileges
pass
def config():
accepted_filters = ['auth', 'auth ....
另外,创建列表的时候,不需要用反斜杠,因为有逗号的存在,直接这样写就可以了:
accepted_filters = ['auth', 'authpriv', 'daemon', 'cron', 'ftp', 'lpr',
'kern', 'mail', 'news', 'syslog', 'user', 'uucp', 'local0', 'local1',
'local2', 'local3', 'local4', 'local5', 'local6', 'local7']
accepted_priorities = ['Emergency', 'Alert', 'Critical', 'Error',
'Warning', 'Notice', 'Info', 'Debug']
这不是错误,但这样做是不太好的习惯。
还有,你在'local1'后面忘了加逗号(我猜你不想让它变成"local1local2")。
8
在定义一个函数后,应该有一个缩进的代码块:
def check_environment():
# check python
# check for syslog
# check for mknod
# check for root privileges
def config():
# .. code
在def check_environment():
之后只有注释,没有实际的代码。这就是错误的原因。如果你想要一个空的函数,可以这样写:
def check_environment():
pass