Hangman1 is a game where a player must guess a word letter by letter. Initially, he gets to see of how many letters the word consists. The player only gets a limited set of chances to guess the word. Every time he guesses a wrong letter, he has one less chance to guess the word. Usually, a drawing of a hangman indicates how many mistakes the player has made, and indirectly, how many chances are left.
Write a class Hangman that has the method guessLetter, among others. Look at the example below to deduct how the class Hangman can work. Also, pay attention to the special cases.
>>> h = Hangman('BaNana')
>>> print(h)
You have 6 more chances.
......
>>> h.guessLetter('x')
Wrong: letter x does not occur in the word.
You have 5 more chances.
......
>>> h.guessLetter('b')
Correct: letter b occurs 1 times in the word.
You have 5 more chances.
B.....
>>> h.guessLetter('n')
Correct: letter n occurs 2 times in the word.
You have 5 more chances.
B.N.n.
>>> h.guessLetter(5)
Traceback (most recent call last):
AssertionError: argument is not a letter
>>> h.guessLetter('kiwi')
Traceback (most recent call last):
AssertionError: argument is not letter
>>> h.guessLetter('e')
Wrong: letter e does not occur in the word.
You have 4 more chances.
B.N.n.
>>> h.guessLetter('n')
Traceback (most recent call last):
AssertionError: letter has already been guessed
>>> h.guessLetter('a')
Correct: letter a occurs 3 times in the word.
Congratulations! You have guessed the word!
BaNana
>>> print(h)
Congratulations! You have guessed the word!
BaNana
>>> h.guessLetter('a')
Sorry, the game is over.
>>> print(h)
Congratulations! You have guessed the word!
BaNana
>>> h = Hangman('strawberry', chances=3)
>>> print(h)
You have 3 more chances.
..........
>>> h.guessLetter('a')
Correct: letter a occurs 1 times in the word.
You have 3 more chances.
...a......
>>> h.guessLetter('b')
Correct: letter b occurs 1 times in the word.
You have 3 more chances.
...a.b....
>>> h.guessLetter('c')
Wrong: letter c does not occur in the word.
You have 2 more chances.
...a.b....
>>> h.guessLetter('y')
Correct: letter y occurs 1 times in the word.
You have 2 more chances.
...a.b...y
>>> h.guessLetter('e')
Correct: letter e occurs 1 times in the word.
You have 2 more chances.
...a.be..y
>>> h.guessLetter('f')
Wrong: letter f does not occur in the word.
You have 1 more chance.
...a.b...y
>>> h.guessLetter('h')
Wrong: letter h does not occur in the word.
Oops, you have been hung.
strawberry
>>> h.guessLetter('h')
Sorry, the game is over.
>>> h.guessLetter('banana')
Sorry, the game is over.
>>> print(h)
Oops, you have been hung.
strawberry