alister.ware at ntlworld.com
Sat Sep 10 14:24:25 CEST 2011
On Sat, 10 Sep 2011 04:20:17 -0700, Tigerstyle wrote:
> Hi guys.
> I'm strugglin with some homework stuff and am hoping you can help me out
> This is the code:
> small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
> def book_title(title):
> """ Takes a string and returns a title-case string. All words EXCEPT
> for small words are made title case unless the string starts with a
> preposition, in which case the word is correctly capitalized.
> >>> book_title('DIVE Into python')
> 'Dive into Python'
> >>> book_title('the great gatsby')
> 'The Great Gatsby'
> >>> book_title('the WORKS OF AleXANDer dumas')
> 'The Works of Alexander Dumas'
> new_title = 
> title_split = title.strip().lower().split()
> for word in title_split:
> if title_split in small_words:
> elif word in small_words:
> return(' '.join(new_title))
> def _test():
> import doctest, refactory return doctest.testmod(refactory)
> if __name__ == "__main__":
> All tests are failing even though I am getting the correct output on the
> first two tests. And the last test still gives me "Of" instead of "of"
> Any help is appreciated.
Ignoring the docttests my process would be to process each word & then
manually capitalize he 1st word, .I would als0 use a comprehension as
makes for cleaner code:-
if word in small_words:
result=[ capitalize(x.lower()) for x in phrase.split(' ') ]
return " ".join(result)
print set_title('the works of alexander dumas')
... I don't like FRANK SINATRA or his CHILDREN.
More information about the Python-list