2013-11-27 82 views
-4

每次我嘗試執行這個腳本,我得到一個錯誤。 代碼:Python錯誤,IndentationError:意外縮進

def getURL(): # Get tokens 
    output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"], stdout=subprocess.PIPE).communicate()[0] 
    return json.loads(output)['streams']['worst']['url'] # Parse json and return the URL parameter 

def build(): # Builds a set of tokens, aka viewers 
    global numberOfSockets 
    global numberOfViewers 
    while True: 
     if numberOfSockets < numberOfViewers: 
      numberOfSockets += 1 
      print "Building viewers " + str(numberOfSockets) + "/" + str(numberOfViewers) 
      urls.append(getURL()) 

錯誤:

File "sript.py", line 19 
    return json.loads(output)['streams']['worst']['url'] # Parse json and return the URL parameter 
    ^
IndentationError: unexpected indent 
+4

這個問題似乎是題外話,因爲它是關於一個基本的語法錯誤 – lifetimes

回答

2

縮進的getURL第二線,使其線與返回語句:

def getURL(): # Get tokens 
    output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"], stdout=subprocess.PIPE).communicate()[0] 
    return json.loads(output)['streams']['worst']['url'] 

記住了Python需要縮進嚴重,因爲它使用它來確定什麼與什麼。

+0

嗯,這使我這個錯誤:導入請求 導入錯誤:沒有模塊名爲請求 – RelativTo

+0

啊我加了請求包,現在它工作正常!非常感謝 – RelativTo

+0

@Downvoter--你至少可以說出你爲什麼低調的原因嗎?我的答案解決了OP的問題。哪裏不對? – iCodez

相關問題