簡體   English   中英

Python縮進混淆,使用4個空格鍵

[英]Python indentation confusion, using 4 space tabs

我有這個小腳本:

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個空格。 為什么會引發錯誤? 還有什么技巧可以避免將來發生這些情況?

函數定義后,應縮進代碼塊:

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

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”)。

缺少一個塊:

def check_environment():

做了

def check_environment():
    pass

而且,由於在定義check_environment() (和config() )之前調用它,您將遇到進一步的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM