Please code this in Python The problem with my code is that each letter I get ri
ID: 3850910 • Letter: P
Question
Please code this in Python
The problem with my code is that each letter I get right, the attempts wrong still decreases. This is the code 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!!")
This is the question.
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.
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.
Explanation / Answer
The code you've written is mostly correct, but has some mistakes which I've pointed below and also corrected them.
1. The main reason for it decrementing count even for a correct guess lies in the fact that you did not ask for a new letter input when the current letter was found (in the ELIF statement), so when it finds a correct letter, it corrects it and removes it from the word[] array. And as the ask input line inside the ELSE part, it will be skipped and one more iteration of the loop will run, where the same letter will come again. Hence now it will be an incorrect letter and will cause the count to be decremented.
2. If there are multiple occurances of a correct letter in the word, only the first one will be taken into consideration. So you need a while loop to include all the occurances together.
3. You are removing the letters from the word[] array. So when all 6 chances are over, you won't have some of the letters while displaying it. So you need a copy of the word, named original_word in my case, to display it once all tries are over.
4. Your loop runs on forever. You will need to break when count==0, or no chances are left to guess.
5. You willalso need to keep a vairable numguesses, to keep a track of the number of guesses you took to guess the correct word.
6. Output-formatting : Always make sure that the output you give is in norms with the question. Like unnecessary stuff like "You Won" etc. is superflous and should be avoided.
I have corrected these mistakes and also attached the code for reference. If you have any doubts still in the answer, feel free to comment and I'll be more than happy to get them cleared.
CODE:
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
original_word = word
guessed = "_" * len(word)
word = list(word)
guessed = list(guessed)
lstGuessed = []
print(''.join(guessed), "(", count, "error-attempts left)")
numguesses = 0
while count>0:
letter = input("Guess a letter:")
numguesses += 1
if letter.upper() in lstGuessed:
letter = ''
print("Already guessed!! (", count, "error-attempts left)")
elif letter.upper() in word:
while(letter.upper() in word):
index = word.index(letter.upper())
guessed[index] = letter.upper()
word[index] = '_'
lstGuessed.append(letter.upper())
print(''.join(guessed), "(", count, "error-attempts left)")
else:
if letter is not '':
lstGuessed.append(letter.upper())
count -=1
print("There is no", letter)
print(''.join(guessed), "(", count, "error-attempts left)")
if '_' not in guessed:
print(''.join(guessed), "(you got it in", numguesses, "guesses)")
break
if count==0 and '_' in guessed:
print("The secret word was ", ''.join(original_word), ". Good luck next time.")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.