| | |
Need help on hangman game on python
![]() |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
0
#2 27 Days Ago
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.
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)
def frame1(): print ("art") while True: if NumberWrong == 3: frame1()
Last edited by AutoPython; 27 Days Ago at 12:20 am.
To P(ython), or not to P(ython)
•
•
Join Date: Dec 2006
Posts: 1,001
Reputation:
Solved Threads: 284
0
#3 27 Days Ago
•
•
•
•
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.
•
•
•
•
It seems like so many people come on to the forums requesting for home works assignments they have no idea on how to do
Last edited by woooee; 27 Days Ago at 12:57 am.
Linux counter #99383
0
#4 27 Days Ago
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
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
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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
0
#7 26 Days Ago
hi
for the difficulty level you could do something like this
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
for the difficulty level you could do something like this
Python Syntax (Toggle Plain Text)
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
![]() |
Other Threads in the Python Forum
- Previous Thread: Building argv from command line
- Next Thread: Using Functions
| Thread Tools | Search this Thread |
address advanced aliased anydbm bash beginner bits calling casino changecolor clear command conversion convert corners count csv cturtle cursor curves definedlines dictionary digital dynamic dynamically events examples excel external file float format frange function gui handling hints homework i/o iframe import input java line linux list lists loan loop matching mouse multiple number numbers obexftp output parsing path port prime programming projects py py2exe pygame python random rational raw_input recursion recursive return scrolledtext searchingfile signal singleton skinning string strings tails terminal text threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode urllib urllib2 valueerror variable web-scrape whileloop word wxpython






