Write a program in PYTHON that will let the user play Hangman as shown in the ex
ID: 3677577 • Letter: W
Question
Write a program in PYTHON that will let the user play Hangman as shown in the examples below.
The user will guess letters until either:
They have correctly guessed all the letters in the word.
They have incorrectly guessed 6 times.
At each turn, display the player’s progress by showing the word they’re guessing in all capital letters, with all non-guessed characters replaced by dashes (“-”).
Suggested Approach
Here’s one possible way to approach this program:
Prompt the user to enter a word
Create a display string with the right number of hyphens (loop and add one for each letter in the target word).
For each letter they guess,
If the letter is not in the target word, count a miss
Otherwise, make a new display string byloop through the target word’s letters
if the letter matches their guess, add it to the new display string
if the letter does not match their guess, copy whatever was in that spot of the old display string over
replace the old display string with the new one
If they have too many misses, tell them they lose; if there are no - left in the display string, tell them they win.
The specific output format is shown in the examples below:
Enter a word: HANGMAN
[-------] You have 6 left, enter a letter: A
Correct!
[-A---A-] You have 6 left, enter a letter: B
Incorrect!
[-A---A-] You have 5 left, enter a letter: C
Incorrect!
[-A---A-] You have 4 left, enter a letter: D
Incorrect!
[-A---A-] You have 3 left, enter a letter: E
Incorrect!
[-A---A-] You have 2 left, enter a letter: F
Incorrect!
[-A---A-] You have 1 left, enter a letter: G
Correct!
[-A-G-A-] You have 1 left, enter a letter: K
You lose! The word was “HANGMAN”
Enter a word: COMPUTER
[--------] You have 6 left, enter a letter: E
Correct!
[------E-] You have 6 left, enter a letter: B
Incorrect!
[------E-] You have 5 left, enter a letter: C
Correct!
[C-----E-] You have 5 left, enter a letter: A
Incorrect!
[C-----E-] You have 4 left, enter a letter: P
Correct!
[C--P--E-] You have 4 left, enter a letter: E
Correct!
[C--P--E-] You have 4 left, enter a letter: R
Correct!
[C--P--ER] You have 4 left, enter a letter: M
Correct!
[C-MP--ER] You have 4 left, enter a letter: A
Incorrect!
[C-MP--ER] You have 3 left, enter a letter: T
Correct!
[C-MP-TER] You have 3 left, enter a letter: O
Correct!
[COMP-TER] You have 3 left, enter a letter: U
You win! The word was “COMPUTER”
Enter a word: Aa
[--] You have 6 left, enter a letter: a
You win! The word was “AA”
Notes
We will only grade you on words and guesses made up of letters in the alphabet.
Convert both the word and the guesses to upper case; word Aa and guess a should say You win! The word was "AA".
If the user guesses a letter twice, handle it the same as the first time. If it initially matched a letter in the string, it should still count as a match and not as a miss. If it was initially a miss, it will count as another miss. This is shown in the examples above.
You may assume that the user only types single-letter guesses.
Explanation / Answer
word = raw_input("Enter a word")
l = len(word)
attempt = 6
ab = []
print "[",
for i in range(l):
print"-",
while(attempt != 0) :
print "]",
print " You have ",
print attempt,
print ", left: enter a letter: ",
c = raw_input()
if c in word :
print
print "Correct!"
print "[",
ab.append(c)
else :
attempt = attempt - 1
if(attempt == 0):
print "you loose, The word was ",word
break
else:
print
print "Incorrect!"
print "[",
k = 0
for i in word :
if i in ab :
print i,
k = k+1
else :
print "-",
if k== l:
print "You Win, The word was",word
break
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.