[Tutor] Please critique my hangman.py program (original) (raw)

Matt Smith smith-matt at tiscali.co.uk
Fri Jul 23 12:10:19 CEST 2004


Hi, Thanks again for all of the help. This list is making learning Python much easier than it could have been. My second Python program has now reached a stage where I'd like to show it to people and get some comments. It's a fairly no-violent version of hangman as no one gets hung and it's impossible to lose. It has taught me alot about using strings, lists and files. Here's the program:

Hangman program by Matt Smith

import random import string

def random_word(word_length): """Returns a random word of length word_length""" file = open("hangman_words.txt","r") word_list = [] for word in file: word = word.strip() if len(word) == word_length: word_list.append(word) file.close() if len(word_list) == 0: return 0 else: return random.choice(word_list)

def print_status (correct_so_far,incorrect,guesses): """Prints the current status of the game""" for i in range(len(correct_so_far)): print correct_so_far[i], print "\nIncorrect letters:", for i in range(len(incorrect)): print incorrect[i], print "\nYou have had",guesses,"guesses so far."

Game title

print "\n**************" print "Hangman Game" print "**************\n"

play_again = "y" while string.lower(play_again) == "y":

# Get a word to guess
while 1:
    try:
        word_length = input("What length word do you want to guess? ",)
        print "Thinking, please wait.."
        word = random_word(word_length)
        if word == 0:
            print "I don't know any words that long!"
        else:
            break
    except:
        print "That's not a proper word length!"
word = string.lower(word)

# Setup list to hold correct letters
correct_so_far = []
for i in range(word_length):
    correct_so_far.append("_")

# Setup some other variables
incorrect = []
guesses = 0
letters_guessed = 0

# Start main game loop.
print "\nI am thinking of a word",word_length,"letters long"
while letters_guessed < word_length:

    # Print status of game on each pass.
    print
    print_status (correct_so_far,incorrect,guesses)

    # Get guess from user
    while 1:
        guess = raw_input("Which letter would you like to try? ")
        guess = string.lower(guess)
        if len(guess) != 1:
            print "You can only guess one letter at a time!"
        elif guess in incorrect or guess in correct_so_far:
            print "You've already tried that letter!"
        elif guess not in ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]:
            print "That's not a valid letter."
        else:
            break
    guesses = guesses + 1

    # Evaluate guess against word
    letter_correct = 0
    for i in range(word_length):
        if guess == word[i]:
            correct_so_far[i] = guess
            letter_correct=1
            letters_guessed = letters_guessed + 1
    if letter_correct == 0:
        incorrect.append(guess)

# Finish the game
print "\nWell done! You guessed the",word_length,"letter word",word,"in",guesses,"goes."
print "You guessed",len(incorrect),"letters incorrectly."
play_again = raw_input("Do you want to play again? (y/n) ",)

print "Goodybye!"

End of Program

Cheers, Matt.



More information about the Tutor mailing list