``````import random
print "Hello",name,"you're about to play Hangman."
print "Everything you get a wrong guess, you will get a {}, if the word were Nicky you'd get 5 tries."
raw_input("Press [[ENTER]]")
words = ["bat","girl","epic","blocks","block","chips","boy","fire","fox","school","community"]
wordnum = random.randrange(1,11,1)
hangword = words[wordnum]
a = len(hangword)
print "The word has",a,"letters"
raw_input("Press [[ENTER]]")
guess = raw_input("Take your guess at a letter in the word:")``````

It's for a hangman game.

Edited by DocBreen: n/a

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by pyTony

``````correct = 0       #Letters correct is zero right now
for letter in hangword: #For every letter in the word they're trying to guess
if guess == letter: #If their guess is equal to that letter
correct += 1    #Add one to 'correct'

if correct > 0:         #Their guess is in there at least once
print guess,"is in the word",correct,"time(s)."
else:                   #'correct' is still zero. No letters match the guess
print "Sorry, that letter is not in the word."``````

Or you can do:

``print hangword.count(guess)``

or

``````>>> hangword ='Tony Veijalainen'
>>> hangword = hangword.lower()
>>> guesses ='a'
>>> print ' '.join((letter if letter in guesses else '_') for letter in hangword)
_  _  _  _  _  _  _  _  _  a  _  a  _  _  _  _
>>> guesses +='e'
>>> print ' '.join((letter if letter in guesses else '_') for letter in hangword)
_  _  _  _  _  _  e  _  _  a  _  a  _  _  e  _
>>> guesses +='n'
>>> print ' '.join((letter if letter in guesses else '_') for letter in hangword)
_  _  n  _  _  _  e  _  _  a  _  a  _  n  e  n
>>> guesses += 'Tony Veijalainen'.lower()
>>> print ' '.join((letter if letter in guesses else '_') for letter in hangword)
t  o  n  y     v  e  i  j  a  l  a  i  n  e  n
>>>``````

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.