lists and list item matches (ghost wodgame)
Baba
raoulbia at gmail.com
Tue Sep 21 18:39:05 EDT 2010
Hi
query level: beginner
as part of a learning exercise i have written code that:
a) asks for a single letter input (assumption: only 1 letter wil be
entered)
b) adds that letter to list1 and then goes through list2 and checks:
1) if any item in list2 starts with list1 > if False: break
2) if list1 == any item in list2 > if True: break
c) start again until 2) is True
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 it be optimised? i have the feeling that my
nesting of IF, WHILE and FOR statements is overkill?
inspired by part IV of
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/pset5.pdf
thanks
Baba
More information about the Python-list
mailing list