2013-10-06 76 views
-4

我是Python新手,正在嘗試一些程序。被困在這一個。Python 3中print-statement語法錯誤

Python 3.2.5 (default, May 15 2013, 23:06:03) [MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> 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) 

SyntaxError: invalid syntax 
>>> 
+1

請檢查你的代碼的格式,並糾正它。縮進在Python中很重要 - 如果它只是一個錯誤複製/粘貼到SO中,那麼只需在您的文章下方編輯並修改即可。請同時發佈您收到的完整堆棧跟蹤和錯誤。 –

+4

值得注意的是,當您位於一組括號內時,不需要反斜槓來續行。 –

回答

1

打印是在Python 3的功能,從而改變

print buildConnectionString(myParams) 

要:

print(buildConnectionString(myParams))