2017-05-31 73 views
-4

代碼返回錯誤@第11行count + = 1; 目標:編寫一個函數tag_count,它將字符串的列表 作爲參數。它應該返回這些字符串中有多少個是XML標記的計數。如果一個字符串是一個XML標記,如果它以左角括號「<」開始 ,並以右尖括號「>」結束,則可以判斷它是否爲XML標記。代碼返回無效語法

def tag_count(string_list): 
    count=0 
    for string in string_list: 
     if (string.endswith('>') or string.startswith('<') == True 
     count=+1 
return count 
+0

你失蹤冒號你的if語句,你應該確保你的縮進始終恆定。你也可能想在if語句中使用'and'而不是'or'。 – BluCode

+0

@BluCode感謝您指出了對您而言可能是微不足道的事情,但我仍然陷入困境,並對此有所幫助。 – RBU

回答

0

你return語句和壓痕都搞砸了:

def tag_count(string_list): 
    count=0 
    for string in string_list: 
     if string.endswith('>') and string.startswith('<'): 
      count += 1 
    return count 
print (tag_count(["test","<item>","test"])) 
+0

再次感謝您指出了對您而言可能是微不足道的事情,但是我被困住了,並且幫助了它。 – RBU