views:

132

answers:

2

Here is my code:

def detLoser(frag, a):
    word = frag + a

    if word in wordlist:
        lost = True
    else:
        for words in wordlist:
            if words[:len(word) == word:
                return #I want this to break out.

            else:   
                lost = True

Where I have a return, I've tried putting in both return and break and both give me errors. Both give me the following error: SyntaxError: invalid syntax. Any Ideas? What is the best way to handle this?

+6  A: 

You've omitted the ] from the list slice. But what is the code trying to achieve, anyway?

foo[ : len( foo ) ] == foo

always!

I assume this isn't the complete code -- if so, where is wordlist defined? (is it a list? -- it's much faster to test containment for a set.)

katrielalex
Yeah, wordlist is a list. What do you mean test containment for a set?
NoahClark
python set is a hashtable, so lookup probably is not as slow as O(log n)
unbeli
@unbeli: true that. I was thinking binary search... should be amortized O(1) I thin?
katrielalex
+2  A: 
def detLoser(frag, a):

    word = frag + a

    if word in wordlist:
        lost = True
    else:
        for words in wordlist:
            if word.startswith(words):
                return #I want this to break out.
            else:   
                lost = True

you can probably rewrite the for loop using any or all eg. ( you should use a set instead of a list for wordlist though)

def detLoser(frag, a):
    word = frag + a
    return word in wordlist or any(w.startswith(word) for w in wordlist)
gnibbler