import random
name = raw_input("Write your name:")
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.

Recommended Answers

All 2 Replies

Whitespace is your friend!

Try adding this to your code.

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 
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.