[Tutor] Help with Hangman program

John Palmer speederpython at gmail.com
Sun Jul 18 12:55:13 CEST 2010


HI

This is my first attempt at writing my own program, (though I have been
through a couple of guides). For my first project I am doing a hangman game.
I have the program working nearly as I want it to be. The only problem is,
that when the user is prompted to enter its chosen word, (I am using a raw
input) and the word is typed in, it shows in the line above, which obviously
makes the game unplayable as the other person therefore can see the word in
front of them. Is there anyway making python hide this.

I have attached the project in a tar.gv as there is a picture which is used
aswell, I will also include the code below aswell (but if you copy and paste
then make sure you remove the image lines)

I am using python 2.6.5
and the os is Ubuntu 10.04

Thanks in advance for your help

Regards

John


The code is:

import Image

#User chooses the word

print
word = raw_input ("Choose your word ... ")


counter_total = 8 #Counter for the number of guesses

j = len(word) #Number of charactors in the word chosen
m = 0

star = "*" * j #Creates a string made up of * of the same number of
charactors as that of j

print star

letter_list = []
position = []

#Guessing the word

while (counter_total > 0) and (word != star):

    counter = 0 #a counter used to tell the user whether the letter was in
the word or not


#User guesses a letter
    print
    letter = raw_input("Choose a letter ... ")

#Checks to see if the letter chosen by the user is in the word

    for k in range(0,j):

        if letter == word[k]:
            letter_list.append(letter)
            position = []
            position.append(k)

            star = star[0:position[0]] + letter +star[position[0]+1:]
#ammend the star variable to include the correctly guessed letters in their
correct places

            print star

            counter = counter + 1

        else:
            counter = counter


#Tell the user if the letter was in the word or not
    if counter > 0 and star != word:
        print "Well done, guess again"
        m = len(letter_list)

    elif star == word:
        print

    else:
        print "Unlucky, guess again"
        counter_total = counter_total - 1
        print "You have %d guesses left" % (counter_total)


#If all letters are chosen then tell the user they are correct
if star == word:
    print "Congrats, you've won"
    print
    print "The word was " + word

#Tell the user if they've lost
if counter_total == 0:
    print "Game over!"
    print
    print "The word was " + word

#If user has lost, then show them picture
    im = Image.open("Hangman.jpg")
    im.show()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100718/c34cd7fc/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Hangman Project.tar.gz
Type: application/x-gzip
Size: 54447 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20100718/c34cd7fc/attachment-0001.bin>


More information about the Tutor mailing list