Introduction to Programming using Python 1st Edition

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

Chapter 10 - Lists - Programming Exercises - Page 355: 10.29

Answer

code

Work Step by Step

# 10.29 (Game: hangman) Write a hangman game that randomly generates a word and # prompts the user to guess one letter at a time, as shown in the sample run. Each # letter in the word is displayed as an asterisk. When the user makes a correct # guess, the actual letter is then displayed. When the user finishes a word, display # the number of misses and ask the user whether to continue playing. Create # a list to store the words, as follows: import random words = ["write", "that", "program", "Hello"] playAgain = 'y' wrong = 0 while playAgain == 'y': w = words[random.randint(0,3)] 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.