2017-04-05 160 views
-5

在代碼下運行時出現語法錯誤'return' outside functionPython錯誤syntaxerror'return'外部函數

# Make the request to publish and check status code 

    print("\tUploading...") 
    server_response = requests.post(publish_url, data=payload, 
            headers={'x-tableau-auth': auth_token, 'content-type': content_type}) 
    _check_status(server_response, 201) 
    return 
+0

您收到的錯誤部分不清楚嗎?當在函數之外使用時,你期望'return'會做什麼? –

+1

您是否也有意將'return'放在代碼之外? XD –

+0

它是一個現成的腳本 - 從Github獲得它。請驗證縮進。謝謝。 –

回答

0

您的錯誤清楚地表明您正在使用函數外的return

由於您的代碼不是Minimal, Complete, and Verifiable example,我們將不得不猜測發生了什麼。

基本上可以有兩種情形:

  1. return指令是不正確縮進。您可以通過縮進指令來修復它,直到它落入您想要返回的函數內。

實施例:

def foo: 
    # do something inside the function 
    # ... 

    return  # notice the indentation 
  • 您的代碼在所有(你貼等的代碼)還沒有發揮作用,所以使用的return不正確。 (這是不可能的,因爲你只是複製/粘貼來自Github的代碼,但它也是一種可能性)。
  • PD:順便說一句,如果你使用return,你實際上並不返回任何東西(比如你發佈的代碼),你可以擺脫的指令。該功能將在到達結束時自動返回。