Hi
query level: beginner
as part of a learning exercise i have written code that must check if a string (as it is build up through raw_input) matches the beginning of any list item and if it equals any list item.
wordlist = ['hello', 'bye']
handlist = []
letter = raw_input('enter letter: ')
handlist.append(letter)
hand = "".join(handlist)
for item in wordlist:
if item.startswith(hand):
while item.startswith(hand):
if hand not in wordlist:
letter = raw_input('enter letter: ')
handlist.append(letter)
hand = "".join(handlist)
else: break
else: break
print 'you loose'
this code works but can my code (and my reasoning/approach) be improved? i have the feeling that my nesting of IF, WHILE and FOR statements is overkill?
thanks
Baba
EDIT Thanks to Dave i was able to considerably shorten and optimise my code.
wordlist = ['hello','hamburger', 'bye', 'cello']
hand = ''
while any(item.startswith(hand) for item in wordlist):
if hand not in wordlist:
hand += raw_input('enter letter: ')
else: break
print 'you loose'
i'm surprised my orignal code worked at all...