Python缩进混淆,使用4个空格选项卡

时间:2011-07-27 10:58:52

标签: python indentation

我有这个小脚本:

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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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”)。

答案 2 :(得分:2)

缺少一块:

def check_environment():

制作

def check_environment():
    pass

您将遇到更多错误,因为您在定义之前调用check_environment()(和config())。