I have no idea how to do this. Please help.

This week's lab is to design a hangman game. Your program should be able to open a text file which contains a list of words (one word on each line of the text file) and read the words into a list to contain all of the possible words.

Your program should then prompt the user to play a game, please provide at least 3 difficultly levels for the user to choose from. (Difficulty applies to the number of wrong answers allowed before the user loses the game)

Once the user has selected a difficulty, a word from the word list should be chosen randomly using python's random library (example of how to use random at bottom of assignment page).

The program should then display stars to screen so the user knows how many letters are in the word (I suggest making a list of stars). Then the user should be prompted for a guess. After each guess, the program should determine if the guess is valid (ie, it should check to see if the guess has been used before....This means you will need to keep track of what letters have been guessed). If the guess is invalid, a message should be displayed telling the user that they have already guessed that letter and prompting the user to try another letter. If the guess is valid, the program should check to see if the letter is in the word.

If the letter is in the word, the list containing stars should be updated so that correct letters are displayed where required. If the letter is not in the word, then the user should be credited with an incorrect guess.

After the guess has been evaluated, a hangman picture should be displayed (based on number of wrong answers and difficulty level...You may use ASCII art or turtle graphics for the picture), the list of incorrect guesses, and current status of the word. The pattern of guessing should continue until the user is able to correctly solve the puzzle, or is "hung" (runs out of incorrect guesses).

Once the game is complete, the user should be asked if they wish to play again. Example of a working hangman game to be demonstrated in lab.

It seems like so many people come on to the forums requesting for home works assignments they have no idea on how to do. Does your teacher realize that you don't have the proper skills to do this? Or maybe you do know you just don't want to work. This just involved a bunch of lists and loops.

First of all, I suggest making a function for each frame if you're going to do ascii. Then it will make it easy to call each frame up. The main game should be held in a while loop. Here's a code example of how it might look.

def frame1():
    print ("art")

while True:
    if NumberWrong == 3:
        frame1()

This week's lab is to design a hangman game. Your program should be able to open a text file which contains a list of words (one word on each line of the text file) and read the words into a list to contain all of the possible words.

If you don't know how to open and read a text file, then look at one of the online tutorials like here http://effbot.org/zone/readline-performance.htm Then get the input from the user, etc. Finally, post the code here that you are not able to complete.

It seems like so many people come on to the forums requesting for home works assignments they have no idea on how to do

This seems to be this year's excuse to get someone else to do it for you. Last year it was "I Googled for hours and can't find the answer". Next year it will be something else.

Member Avatar for masterofpuppets

hi
first try to solve the problem yourself and then if you are stuck at some point post your code here and we'll be happy to help. At least try to show some effort before giving up so easily :) It would be much more helpful if you solved the problem rather than someone else posting their solution here.
Try to make a plan of what things to do first, set up the main functions and use them in the game loop :) :)

import random
game =
star = '*'
play = raw_input("Do you want to play Hang Man? ")
difficulty = raw_input("Easy, Medium, or Hard difficulty? ")
guess = raw_input("Guess a letter: ")

while play !='n':
if play == 'y':
if difficulty == ("Easy"):
guess * 3
if difficulty == ("Medium"):
guess * 4
if difficulty == ("Hard"):
guess * 5
print star * len(words)
guess
guess = guess.strip()
if guess in words:
print 'Correct'
print star *len(words)
print guess
elif guess == '':
print 'Incorrect'
print star * len(words)
print guess

What do I put for the difficulty levels to change the number of guesses?

Member Avatar for masterofpuppets

hi
for the difficulty level you could do something like this

def getDifficultyLevel( i ):
    if i == "easy":
        return 15
    elif i == "medium":
        return 10
    elif i == "hard":
        return 5
    else:
        return -1 #Error in the input.

difficulty = raw_input( "Choose your difficulty level - easy, medium, hard >> " )
guessesLeft = getDifficultyLevel( difficulty )

#and for the main loop you could have
while guessesLeft > 0:
    #Play the game here...

#outside the loop check if the user guessed the word. You could have a boolean variable 'win' or something and if the user guesses the whole word set 'win' to True...

you haven't read the words from a text file yet. Try to do that first in order to have something to work with :) hope this gives you some ideas :)

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.