Question
Please code this in Python The problem with my code is that each letter I get right, the attempts wrong still decreases Final Project Hangman
Please code this in Python
The problem with my code is that each letter I get right, the attempts wrong still decreases
Final Project Hangman game
You will build a text version of the Hangman game. The player has 6 incorrect guesses (head, body, 2 legs, and 2 arms) before he loses the game.
Load a word list of 10 or more words and randomly pick a word from it. Write the logic for enabling the user to guess a letter and display the state of the word to the user with only the correctly guessed letters revealed and remaining letters displayed as dashes. After every guess display the updated state of the word to the user, and let the user know how many error-attempts he has remaining. Keep track of letters guessed by the user. The user either guesses the word correctly in 6 or less error-attempts or he exhausts his attempts. Congratulate the user if he guesses the word correctly in 6 or fewer error-attempts otherwise reveal the secret word and wish him good luck next time.
(Optional bonus): If the user enters the same letter again, let him know that he has already guessed the letter and do not penalize that attempt.
Grading Rubric
Points | Task |
4 | Display state of the word with correctly guessed letters revealed and remaining letters displayed as dashes. |
2 | Let the user know how many more error-attempts he has. |
1 | Final message letting the user know how many guesses the user took to guess the word correctly, or revealing the secret word and wishing good luck next time. |
Example output for :
Welcome to Hangman!
_ _ _ _ _ (6 error-attempts left)
Guess a letter: a
_ a _ _ _ (6 error-attempts left)
Guess a letter: p
_ a p p _ (6 error-attempts left)
Guess a letter: e
There is no e
_ a p p _ (5 error-attempts left)
Guess a letter: h
h a p p _ (5 error-attempts left)
Guess a letter: y
h a p p y (you got it in 5 guesses)
--
If the user is unable to guess in 6 or less error-attempts, reveal the word and display,
The secret word was happy. Good luck next time.
This is what I have so far
import random wordlist = ['RED','BLUE','GREEN','PURPLE','YELLOW','BLACK','WHITE','ORANGE','BROWN','SILVER'] randWord = random.choice(wordlist) count = 6 if __name__ == '__main__': print("Welcome to hangman!!") word = randWord guessed = "_" * len(word) word = list(word) guessed = list(guessed) lstGuessed = [] letter = input("Guess a letter: ") while True: if letter.upper() in lstGuessed: letter = '' print("Already guessed!!", count, "error-attempts left") elif letter.upper() in word: index = word.index(letter.upper()) guessed[index] = letter.upper() word[index] = '_' else: print(''.join(guessed)) if letter is not '': lstGuessed.append(letter.upper()) count -=1 print("There is no", count, "error-attempts left") letter = input("Guess a letter:") if '_' not in guessed: print("You won!!")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started