lists and list item matches (ghost wodgame)

B

Baba

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/electric...d-programming-fall-2008/assignments/pset5.pdf

thanks
Baba
 
N

nn

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 ofhttp://ocw.mit.edu/courses/electrical-engineering-and-computer-scienc...

thanks
Baba

Yes it is overkill. Especially the else:break from the while loop
makes it difficult to follow the logic. Also the program breaks down
if I use the following word list:

wordlist = ['hello', 'hamburger', 'bye']enter letter: h
enter letter: a
you loose

I am not going to post any spoilers but I wrote your program using one
while loop and one generator expression for a total of 5 lines. My
version might be a bit too advanced but you should still be able to do
it using only one while, one for and one if instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top