[Tutor] Stuck on Challenge in Google Python Class

Alan Gauld alan.gauld at btinternet.com
Wed Jan 22 12:40:57 CET 2014


On 22/01/14 04:24, Adam Hurwitz wrote:

> A. match_ends
> # 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.

Notice that you are asked to *return* a number not print it.
Also notice that the condition is an AND not an OR.

> def match_ends(words):
>    numberStrings = [ ]
>    answer = ' '
>
>    for string in words:
>      if len(string) >=2 or string[0] == string[-1]:
>        numberStrings.append(string)
>      else:
>        return ' '

Notice that this return will throw you out of the function so
you don't process any more strings. You might like to look
at 'continue' as an alternative.

>    answer = len(numberStrings)
>    print answer

And here you print the result not return it.
So the return value from your function as is will be None or ''

But you are quite close to the solution.

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list