Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 13 - Files and Exception Handling - Programming Exercises - Page 472: 13.7

Answer

code

Work Step by Step

# 13.7 (Game: hangman) Rewrite Exercise 10.29. The program reads the words stored # in a text file named hangman.txt. Words are delimited by spaces. import random file = open('Words', 'r') words = file.read().split() playAgain = 'y' wrong = 0 while playAgain == 'y': w = words[random.randint(0, len(words) - 1)] w = list(w) res = ['*'] * len(w) print("(Guess) Enter a letter in word", ''.join(res), end=' ') f = 0 while True: guess = input() if guess not in res: if guess in w: for i in w: if i == guess: res[w.index(i)] = guess w[w.index(i)] = '_' if res.count('*') == 0: f = 1 break if f == 1: break else: wrong += 1 print("\t", guess, "is not in the word") else: print("\t", guess, "is already in the word") print("(Guess) Enter a letter in word", ''.join(res), end=' ') print("The word is", ''.join(res)) print("You have", wrong, "wrong guesses") wrong = 0 playAgain = input("Do you want to play again (y or n) ").lower()
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.