On Sep 22, 3:39 pm, Baba <raoul...@gmail.com> wrote: > On Sep 22, 9:18 pm, Baba <raoul...@gmail.com> wrote: > > > > > On Sep 22, 3:38 pm, nn <prueba...@latinmail.com> wrote: > > > > On Sep 21, 6:39 pm, Baba <raoul...@gmail.com> wrote: > > > > > Hi > > > > > query level: beginner > > > > > as part of a learning exercise i have written code that: >
<snip problem description> > > wordlist = ['hello', 'bye'] > > hand = '' > > for item in wordlist: > > if item.startswith(hand): > > while item.startswith(hand): > > if hand not in wordlist: > > hand += raw_input('enter letter: ') > > print hand > > else: break > > else: break > > print 'you loose' > > > But i can't figure out why it won't work when adding the extra word. > > Thanks by the way, it taught me not to be too confident when things > > SEEM to work... > <snip> > looking at my original code again i realise that having a raw_input > inside a FOR loop is flawed per se (at least for my purposes) so i > will just assume that i was taking the wrong approach initially. No > point in analysing it further. Thanks for your help. > Yes, the overall structure using the 'for' loop was a bit awkward, and your second attempt using 'any' is much clearer; but I'd say it's still worth seeing how your original logic failed. It has to do with the action of the 'break's. Ask yourself, which code block does the 'if <> else: break' contained /within/ your while loop, break you out of? And which code block does the 'while <> else: break' break you out of? One other thing... it's 'you LOSE', not 'you LOOSE'!!!!! (personal pet peeve ;-) ) Cheers - Chas -- http://mail.python.org/mailman/listinfo/python-list