Doctest failing
Peter Otten
__peter__ at web.de
Sat Sep 10 07:47:04 EDT 2011
Tigerstyle wrote:
> I'm strugglin with some homework stuff and am hoping you can help me
> out here.
>
> This is the code:
>
> small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
> new_title = []
> title_split = title.strip().lower().split()
> for word in title_split:
> if title_split[0] in small_words:
> new_title.append(word.title())
> elif word in small_words:
> new_title.append(word.lower())
> else:
> new_title.append(word.title())
The logic of the for-loop is flawed; the expression
title_split[0] in small_words
will always evaluate to True if the first word is a "small word", even when
the loop is already past the first word. You can work around that with a
flag along these lines
first = True
for word in title_split:
if first:
# special treatment for the first word
first = False
else:
# put checks for all words but the first here
new_title.append(fixed_word) # assuming you have stored the titlecased
# or lowercased word in the fixed_word
# variable
More information about the Python-list
mailing list