use python please!!! thanks You must have at least 1 user defined function per p
ID: 3762696 • Letter: U
Question
use python please!!! thanks
You must have at least 1 user defined function per problem.
• You may not use global variables.
• You may not import any modules unless otherwise specified in the problem
For this problem you will be implementing the game hangman. In hangman one
person chooses a secret word and and displays _ representing the position of each character. The
other person guesses letters in the word in an attempt to figure out what it is. If the character is
in the secret word the then all positions that contain that character are revealed. If the character
is not in the secret word then a body part of the hang man is drawn. The second person
continues guessing letters until either they reveal the entire secret word or until the hangman
figure is complete. If the word is completely revealed the second player wins. If the hangman is
completed the second player loses. Write a program called hangman.py that implements the
hangman game. Here is Wikipedia's article on hangman if you need more information on how
the game is played.
1. The second player has 7 guesses as there are 7 pieces to the hangman figure. The pieces are
added to the figure in the following order: rope, head, torso, left arm, right arm, left leg,
right leg.
2. At the beginning of the game the first player should be asked to enter the secret word. All
characters are legal excluding ? and white space characters (tab, space, newline). After the
user enters a word you should display 30 newlines so as to hide the secret word.
3. At the beginning of each round you should display in this order: The hanged man, the
partially guessed secret word, the characters guessed so far.
4. For the secret word, the characters that have not been guessed should be displayed as ?
5. Guesses for the characters in the secret word should be case insensitive. Store the entered
secret word in lowercase and convert all guessed characters to lower case.
6. Leading and trailing white space should be removed from the user's guess before checking it
7. The user must enter a guess. If they do not they should be told that they must enter a guess
and receive the prompt to enter the next character again. This should continue until the user
enters a new character.
8. If the user guesses a character that they have already guessed then they should be told that
they already guessed that character and be given the opportunity to guess again. This should
continue until the user enters a new character. These guesses should not be counted against
the user.
9. The user can only guess 1 character at a time. If the user enters more than 1 character they
should be told that they can only enter 1 character and be given the opportunity to guess
again. This should continue until the user enters a new character.
10. The letters that have been guessed so far should be displayed in sorted order.
11. If the user guesses the secret word they should be told that they guessed the secret word.
12. If the user does not guess the secret word they should be told that they failed to guess the
word and the word should be revealed.
13. You MUST USE FUNCTIONS for this program as it will make things much easier.
The are two examples of playing the hangman game below.
Example(The extra newlines after the secret word are not shown for space reasons):
Please enter a word to be guessed
that does not contain ? or white space: hello
?????
So far you have guessed:
Please enter your next guess: h
h????
So far you have guessed: h
Please enter your next guess: t
|
h????
So far you have guessed: h, t
Please enter your next guess: y
|
0
h????
So far you have guessed: h, t, y
Please enter your next guess: t
You already guessed the character: t
Please enter your next guess: y
You already guessed the character: y
Please enter your next guess: hi
You can only guess a single character.
Please enter your next guess: joke
You can only guess a single character.
Please enter your next guess: l
|
0
h?ll?
So far you have guessed: h, l, t, y
Please enter your next guess: u
|
0
|
h?ll?
So far you have guessed: h, l, t, u, y
Please enter your next guess: (
|
0
/|
h?ll?
So far you have guessed: (, h, l, t, u, y
Please enter your next guess: e
|
0
/|
hell?
So far you have guessed: (, e, h, l, t, u, y
Please enter your next guess: o
You correctly guessed the secret word: hello
Example (The extra newlines after the secret word are not shown for space reasons):
Please enter a word to be guessed
that does not contain ? or white space: don't ? me
Please enter a word to be guessed
that does not contain ? or white space: me and you
Please enter a word to be guessed
that does not contain ? or white space: ?the-system
Please enter a word to be guessed
that does not contain ? or white space: back--dash
??????????
So far you have guessed:
Please enter your next guess: joke
You can only guess a single character.
Please enter your next guess:
You must enter a guess.
Please enter your next guess: b
b?????????
So far you have guessed: b
Please enter your next guess: b
You already guessed the character: b
Please enter your next guess: a
ba?????a??
So far you have guessed: a, b
Please enter your next guess: a
You already guessed the character: a
Please enter your next guess: b
You already guessed the character: b
Please enter your next guess: bathroom
You can only guess a single character.
Please enter your next guess: 1
|
ba?????a??
So far you have guessed: 1, a, b
Please enter your next guess: /
|
0
ba?????a??
So far you have guessed: /, 1, a, b
Please enter your next guess: -
|
0
ba??--?a??
So far you have guessed: -, /, 1, a, b
Please enter your next guess: r
|
0
|
ba??--?a??
So far you have guessed: -, /, 1, a, b, r
Please enter your next guess: a
You already guessed the character: a
Please enter your next guess: q
|
0
/|
ba??--?a??
So far you have guessed: -, /, 1, a, b, q, r
Please enter your next guess: 9
|
0
/|
ba??--?a??
So far you have guessed: -, /, 1, 9, a, b, q, r
Please enter your next guess: ^
|
0
/|
/
ba??--?a??
So far you have guessed: -, /, 1, 9, ^, a, b, q, r
Please enter your next guess: z
|
0
/|
/
You failed to guess the secret word: back—dash
Explanation / Answer
# Read input from stdin and provide input before running code
while True:
name = raw_input('Please enter a word to be guessed that does not contain ? or white space: ')
if '?' not in name and ' ' not in name:
break
print name
print name
for i in range(30):
print ' '
res = []
res1=[]
for i in range(len(name)):
res1.append('?')
while True:
guess = raw_input('Please enter your next guess: ')
if len(guess) != 1:
print 'You can only guess a single character '
continue;
if guess in res:
print 'you have alredy guessed '+ guess
continue
res.append(guess)
for i in range(len(name)):
if guess == name[i]:
res1[i]=guess
print 'So far you have guessed:'
print res
print ''.join(res1)
if ''.join(res1) == name:
print 'you have guessed correctly '
break
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.