簡體   English   中英

IndentationError:預期縮進塊

[英]IndentationError: expected an indented block

我剛剛開始使用python。 我在Ubuntu中執行Mark Pilgrim在'Dive into Python'中給出的一個簡單程序。 該計划如下:

def buildConnectionString(params):

    """Build a connection string from a dictionary of parameters.
    Returns string."""
    return ";".join(["%s=%s" % (k, v) for k, v in params.items()])
    if __name__ == "__main__":
    myParams = {"server":"mpilgrim", \
    "database":"master", \
    "uid":"sa", \
    "pwd":"secret" \
    }
    print buildConnectionString(myParams)

但它顯示錯誤如下:

  File "./1.py", line 3
    Returns string."""
                     ^
IndentationError: expected an indented block

我嘗試了一些東西,比如在第3行返回前面給出一個空格,然后使用制表符而不是空格。 任何人都可以幫助我找出錯誤是什么,為什么它來了,等等,還有一些簡單的教程,可以繼續。

提前致謝..

試試這樣:

def buildConnectionString(params):
    """Build a connection string from a dictionary of parameters.
    Returns string."""
    return ";".join(["%s=%s" % (k, v) for k, v in params.items()])

if __name__ == "__main__":
    myParams = {"server":"mpilgrim", \
    "database":"master", \
    "uid":"sa", \
    "pwd":"secret" \
    }
    print buildConnectionString(myParams)

順便說一句:你了解結構嗎? 函數, if __name__=="__main__":塊等?

為什么不閱讀Python文檔? 它可能有所幫助。 ;)

http://docs.python.org/2/reference/lexical_analysis.html#indentation

暫無
暫無

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

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