944,070 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1632
  • Python RSS
Oct 29th, 2009
0

Need help on hangman game on python

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
henks83 is offline Offline
3 posts
since Oct 2009
Oct 30th, 2009
0
Re: Need help on hangman game on python
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.

Python Syntax (Toggle Plain Text)
  1. def frame1():
  2. print ("art")
  3.  
  4. while True:
  5. if NumberWrong == 3:
  6. frame1()
Last edited by AutoPython; Oct 30th, 2009 at 12:20 am.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Oct 30th, 2009
0
Re: Need help on hangman game on python
Quote ...
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.

Quote ...
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.
Last edited by woooee; Oct 30th, 2009 at 12:57 am.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Oct 30th, 2009
0
Re: Need help on hangman game on python
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
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Oct 30th, 2009
0

So far

import random
game = ['hangman']
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
henks83 is offline Offline
3 posts
since Oct 2009
Oct 30th, 2009
0

difficulty levels

What do I put for the difficulty levels to change the number of guesses?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
henks83 is offline Offline
3 posts
since Oct 2009
Oct 30th, 2009
0
Re: Need help on hangman game on python
hi
for the difficulty level you could do something like this

Python Syntax (Toggle Plain Text)
  1. def getDifficultyLevel( i ):
  2. if i == "easy":
  3. return 15
  4. elif i == "medium":
  5. return 10
  6. elif i == "hard":
  7. return 5
  8. else:
  9. return -1 #Error in the input.
  10.  
  11. difficulty = raw_input( "Choose your difficulty level - easy, medium, hard >> " )
  12. guessesLeft = getDifficultyLevel( difficulty )
  13.  
  14. #and for the main loop you could have
  15. while guessesLeft > 0:
  16. #Play the game here...
  17.  
  18. #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
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: OpenCV and video capturing
Next Thread in Python Forum Timeline: Using Functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC