Why For Loop Skips the Last element?

Denis McMahon denismfmcmahon at gmail.com
Thu Jan 1 15:24:37 CET 2015


On Thu, 01 Jan 2015 02:55:49 +0530, Thuruv V wrote:

> Hi all,
> 
> Here's the list. .
> 
> inlist = ["Fossil Women's Natalie Stainless Steel Watch Brown (JR1385)",
> 'Balmer Swiss Made Veyron Mens Watch: Black Band/ Black Dial
> (62625241)',
> 'Fortune NYC Ladies Quilted Dial Watch: Brown',
> 'Jeanne Collection w/ Swarovski Elements Watch: Dark Purple Band
> (62623659)',
> 'Swiss Legend Commander Chronograph: Watch Orange/Black
> (SL-10067-01-ORS)',
> 'Debussy Ladies Watch: Brown/Gray/Silver 14070 - 62625292',
> '#2 Swiss Legend Commander Chronograph: Green (SL-10067-BB-017)',
> 'Auguste Jaccard 3 Timezone Watch: Black Band/ Gray & Black Dial
> (62625184)'
> ]
> 
> My code :
> i = 0 while i <= len(inlist):
>     if 'watch' in str(inlist[i]).lower() or 'watches' in
> str(inlist[i]).lower():
>         if 'women' in str(inlist[i]).lower() or 'female' in
> str(inlist[i]).lower() or 'ladies' in str(inlist[i]).lower():
>             print 'FEMale Watch', inlist.index(i),str(i)
>         elif 'men' in str(inlist[i]).lower() or 'male' in
> str(inlist[i]).lower() or 'chronograph' in str(inlist[i]).lower():
>             print 'Male Watch',inlist.index(i),str(i)
>         i = next(inlist)

..........^

If the indents are correct, i will only increment if watch or watches are 
found. Your last but 1 item doesn't match these strings (chronograph 
instead of watch).

It would still be better as others have posted to use a for loop.

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list