#i couldnt find the difference in the code
>>> def match_ends(words):
# +++your code here+++
count=0
for string in words:
if len(string)>=2 and string[0]==string[-1]:
count=count+1
return count
>>> match_ends(['', 'x', 'xy', 'xyx', 'xx'])
2
>>>
>>> def match_ends(words):
# +++your code here+++
count=0
for string in words:
if string[0]==string[-1] and len(string)>=2:
count=count+1
return count
>>> match_ends(['', 'x', 'xy', 'xyx', 'xx'])
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
match_ends(['', 'x', 'xy', 'xyx', 'xx'])
File "<pyshell#25>", line 5, in match_ends
if string[0]==string[-1] and len(string)>=2:
IndexError: string index out of range
if len(string)>=2 and string[0]==string[-1]:
除了第一个函数和 if string[0]==string[-1] and len(string)>=2:
第二个函数中的 if 条件外,我找不到代码中的差异