View Single Post
Old May 11th, 2006, 10:16 AM   #6
commodore
Programmer
 
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0 commodore is an unknown quantity at this point
I haven't tried hydroxide's version yet. Arevos' post helped me a lot but I still have a problem. It doesn't exit the biggest while loop. If I guess the word it will still offer me to guess it and if I have lost all my lives it still does so.

The current code looks like this:

import random
import sys
import time
filer=open('words.txt', 'r')

words=filer.readlines() # puts the words in a list
orgwordstring=random.choice(words).strip() # chooses a random word from the list
orgword=list(orgwordstring)

newword=list(orgword[0]+((len(orgword)-2)*'_')+orgword[-1])

life=10
while newword!=orgword or life!=0:
    print 'The word is: %s' % (' '.join(newword))
    print ''
    what=str(raw_input('Type in a letter that you think should be in that word or leave empty if you want to quit: '))
    if what=="":
        sys.exit()
    if what not in orgword:
        life=life-1
        print 'Sorry this letter isn\'t included in the word. You have lost a life. You have %d lives left' % (life)
    else:
        for position, letter in enumerate(orgword):
            if letter == what:
                newword[position]=letter

filer.close()
if life!=0:
    'Gongratulations! You have guessed the word! It is ', newword
    wordloc=''.join(words).find(orgword)
    del words[wordloc]  # deletes the guessed word from the list
    words='\n'.join(words)                     
    filew=open('words.txt', 'w')
    filew.write(words)  # writes the words back to the file, now without the guessed word
    filew.close()
else:
    'You have 0 lives left. You didn\'t guess the word.'
time.sleep(10)
sys.exit()
commodore is offline   Reply With Quote