unfininished
Maxim Ryan <wiseone6 <at> hotmail.com>
2004-04-01 11:50:09 GMT
can u help me fininish this program? as you run it u will see that afer the blanks are filled the prog. doesnot end and i also need to subtract the used letters from the alphabet and count how many turns are left.(i am giving the user 6 turns to guess the word
Protect your PC -
Click here for McAfee.com VirusScan Online
import random
import string
def getlist():
file=open("A:\hanging.txt","r")
words=file.read()
split=string.split(words,'\n')
return split
def pickword(split):
a=random.random()*len(split)
a=int(a)
b=split[a]
print b
return b
alphabet=string.ascii_lowercase
def unguessed(let,alphabet):
area=string.find(alphabet,let)
alphabet=alphabet[:area]+alphabet[area+1:]
print "unguessed:"+alphabet
return alphabet
def question(b,alphabet):
question=raw_input("press A to guess one letter at a time or B to guess
the word: ")
while question<>"a" and question<>"A" and question<>"b" and
question<>"B":
print " please enter a or b "
question=raw_input("press A to guess one letter at a time or B to
guess the whole word: ")
if question=="a" or question=="A":
partone(b,alphabet)
elif question=="b" or question=="B":
playgame(b,alphabet)#return #getguess(b)
givenletters = []
def underscores(b):
output = []
for letter in b:
if letter in givenletters:
output.append(letter)
else:
output.append('_')
print "Word:"
print string.join(output, ' ')
def safeinput(b):
underscores(b)
let= raw_input("enter a letter: ")
if let== "":
print "please enter a value"
return safeinput(b)
if len(let)>1:
print" please enter a single lowercase letter"
return safeinput(b)
for char in let:
if string.find(string.lowercase,char) == -1:
print "please enter a single lowercase letter"
return safeinput(b)
return let
def printlet(let,b,alphabet):
show=givenletters.append(let)
if let not in b:
print"wrong guess"
unguessed(let,alphabet)
win(b,alphabet)
choice(b,alphabet)
if let in b:
print"good guess"
unguessed(let,alphabet)
win(b,alphabet)
choice(b,alphabet)
def getguess(b):
ask=raw_input("try to guess the word: ")
if ask=="":
print "please enter a value"
underscores(b)
return getguess(b)
for char in ask:
if string.find(string.lowercase,char)==-1:
print "please enter a lowercase letter"
underscores(b)
return getguess(b)
return ask
def testword(ask,b,alphabet):
if ask ==b:
print " CONGRATULATIONS U MADE A FLUKE "
return loop(alphabet)
return
else:
print" better luck next time"
print "the word was" +" " + b
return loop(alphabet)
return
def win(b,alphabet):
for char in b:
return b
for char in givenletters:
return givenletters
if b==givenletters:
print"dfdfd"
loop(alphabet)
def loop(alphabet):
play=raw_input("DO YOU WANNA PLAY AGAIN? y/n: ")
while play<>"y" and play<>"Y" and play<>"n" and play<>"N":
print " please enter y or n "
play=raw_input("DO YOU WANNA PLAY AGAIN? y/n: ")
if play=="y" or play=="Y":
start(alphabet)
else:
print "THE END"
return
def playgame(b,alphabet):
#underscores(b)
guess=getguess(b) #you guesses the entire word
testword(guess,b,alphabet) #compares word with guess
def partone(b,alphabet):
safe=safeinput(b)
printlet(safe,b,alphabet) #display entered letters in underscores
def choice(b,alphabet):
underscores(b) #print underscores
question(b,alphabet) #choice between guessing word or
each letter
def start(alphabet):
a=getlist()
b=pickword(a)
choice(b,alphabet)
start(alphabet) #start
_______________________________________________
Tutor maillist - Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor