I've made my own hangman game with which you can compare yours. I don't know if you're familiar with functions, but it should be fairly easy to see what's going on.
import random
def get_guess(used_letters, word, word_hidden):
while True:
guess = get_string("Guess a letter or the entire word.").lower()
if not guess.isalpha():
print "Cannot use '%s' as the guess (failed at guess.isalpha()). Try again." % guess
elif guess == word:
return "$word"
elif len(guess) != 1:
return "$wrong"
continue
elif len(guess) == 1:
error = False
for letter in used_letters:
if guess == letter:
print "You've already guessed '%s'! Try again." % guess,
error = True
break
for letter in word_hidden:
if guess == letter:
print "You've already used '%s'! Try again." % guess,
error = True
break
if error == True:
continue
return guess
def get_string(message):
print str(message) + "\n"
while True:
try:
the_string = raw_input("> ")
except ValueError:
print "ValueError exception encountered! Try again."
continue
if the_string == "":
print "Error! Empty string! Try again."
continue
break
return the_string
def play(word):
word_hidden = "_"*len(word[2:])
used_letters = []
tries_left = 8
while True:
print "\n\t\t\t%s\nValue: %s points\n" % (word_hidden, word[0:2])
if used_letters != []:
print "---- Used letters:", used_letters
print ""
guess = get_guess(used_letters, word[2:], word_hidden)
print "\n\n"
if guess == "$word":
print "Correct!"
guessed_letters = []
for letter in word_hidden:
if letter.isalpha():
used = False
for item in guessed_letters:
if letter == item:
used = True
if used == False:
guessed_letters.append(letter)
if len(guessed_letters) == 0:
print "\nMEGABONUS!!!\n"
return int(word[0:2])*5
elif len(used_letters) == 0:
print "\nSUPER BONUS!!\n"
return int(word[0:2])*3
else:
print "\nBonus!\n"
return int(word[0:2])*2 - len(guessed_letters)
i = 0
guessed = False
if guess != "$wrong":
for letter in word[2:]:
if guess == letter:
word_hidden = word_hidden[:i] + guess + word_hidden[i+1:]
guessed = True
i += 1
if word_hidden == word[2:]:
print "Correct! You found the word '%s'!" % word[2:]
return int(word[0:2])
if guessed == True:
print "Found one!"
else:
print "Wrong!",
tries_left -= 1
if tries_left > 1:
print "Tries left:", tries_left
elif tries_left == 1:
print "This is your last try!!"
elif tries_left == 0:
print "\nYou didn't find the word '%s'..." % word[2:]
return 0
if guess != "$wrong":
used_letters.append(guess)
def wait(message):
raw_input(message)
def main():
print "Initializing...\n"
fwords = open("hmwl.hang", "r")
shuffle = random.shuffle
wordlist = []
for line in fwords.read().split("\n"):
wordlist.append(line)
shuffle(wordlist)
fwords.close()
score = 0
extra = 0
continues = 3
while continues >= 0:
word = wordlist.pop()
points = play(word)
if points == 0:
continues -= 1
else:
score += points
extra += points
print "\nScore: %d points + %d points won = %d points" % (score-points, points, score)
while extra >= 100:
continues += 1
print "\nExtra chance awarded! Chances left:", continues
extra -= 100
if continues == 1:
print "\nThis is your last chance!!"
elif continues == 0:
print "\nGame over."
break
else:
print "\nChances left:", continues
wait("\nPress enter to continue...")
print "\n\n\n"
wait("Exiting...")
if __name__ == '__main__':
main()
My wordlist was over 7MB, so I can't attach it, but here's just a short (and rather silly) one if you want to test the programme (I generated the values with another programme). Just put this in a file called "hmwl.hang":
09animal
22antidisestablishmentarianism
08apple
28bababadalgharaghtakamminarronnkonnbronntonnerronntuonnthunntrovarrhounawnskawntoohoohoordenenthur
07cat
07dog
22ethylenediaminetetraacetates
21floccinaucinihilipilification
09fruit
08game
10hangman
23honorificabilitudinitatibus
33lipsmackinthirstquenchinacetastinmotivatingoodbuzzincooltalkinhighwalkinfastlivinevergivincoolfizzin
28llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch
10monkey
10orange
08pear
24pneumonoultramicroscopicsilicovolcanoconiosis
10program
10python
20sesquipedalianism
28sodiummetadiaminoparadioxyarsenobenzoemethylenesulphoxylate
25supercalifragilisticexpialidocious
28twoallbeefpattiesspecialsaucelettucecheesepicklesonionsonasesameseedbun
09waffle
(Edit: Some of the words are fairly long and the forum code inserts a space there. If you quote my post, it'll be easier to copy-paste)