我寫簡單的Python代碼:編程錯誤
Question:
Given a list of strings, return the count of the number of
# strings where the string length is 2 or more and the first
# and last chars of the string are the same.
解決我的工作:
def match_ends(words):
for items in words:
count = 0
los = len(items)
first_char= items[0]
last_char= items[los-1]
if los >=2 and first_char is last_char:
count = count+1
else:
count = count
print count
return
def main():
print 'match_ends'
match_ends(['aba', 'xyz', 'aa', 'x', 'bbb'])
我不斷獲取答案爲1的時候,我覺得它是沒有完全循環。錯誤在哪裏
我不喜歡的樣子那是'是'。嘗試使用'=='代替。哦,並且在循環之外移動count == 0。 – Kevin 2014-12-02 18:41:33
你是否打算在循環的每次迭代中將'count'重置爲'0'? – Celeo 2014-12-02 18:42:43
'else:count = count'沒用。 – user2097159 2014-12-02 18:43:33