Using Python. You will build a text version of the Hangman game. The player has
ID: 3906100 • Letter: U
Question
Using Python.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.
Create 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.
If the user enters the same letter again, let him know that he has already guessed the letter and do not penalize that attempt.
Update your code so it loads the initial word-list from a file named words.txt. If there's an error reading from the file, offer the user a backup secret word. Using Python.
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.
Create 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.
If the user enters the same letter again, let him know that he has already guessed the letter and do not penalize that attempt.
Update your code so it loads the initial word-list from a file named words.txt. If there's an error reading from the file, offer the user a backup secret word.
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.
Create 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.
If the user enters the same letter again, let him know that he has already guessed the letter and do not penalize that attempt.
Update your code so it loads the initial word-list from a file named words.txt. If there's an error reading from the file, offer the user a backup secret word. 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.
Create 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.
If the user enters the same letter again, let him know that he has already guessed the letter and do not penalize that attempt.
Update your code so it loads the initial word-list from a file named words.txt. If there's an error reading from the file, offer the user a backup secret word.
Explanation / Answer
import random
name = "words23.txt"
file = None
list = []
try:
file = open(name,"r")
except:
print("Error opening file")
index = 0
count = 0
if file != None:
for line in file:
list.append(line)
count = count + 1
index = random.randint(0,count-1)
else:
print("Error opening file")
list.append("happy")
incorrect_guess = 0
print("Welcome to Hangman!")
#count = len(list[index])
count = 6
tryCount = 0
line = []
for i in range(len(list[index])):
if (list[index][i] != ' '):
line.append('_')
while True:
str1 = ""
for i in range(len(line)):
str1 = str1 + line[i] + " "
print(str1 , "(",count,"error attempts left )")
a = input("Guess a letter? ")
tryCount = tryCount + 1
found = 0
cnt = 0
for i in range(len(list[index])):
if a[0] == list[index][i]:
line[i] = a[0]
found = 1
if found == 0:
print("There are no ",a[0],"'s")
count = count - 1
if count == 0:
print("The secret word was",list[index],"Good luck next time")
break
if '_' not in line:
print(list[index],"(you got it after picking",tryCount,"letters)")
break
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.