Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USE! PYTHON The goal of this assignment is to use tkinter to produce a class han

ID: 3662006 • Letter: U

Question

USE! PYTHON

The goal of this assignment is to use tkinter to produce a class hangman that allows the user to play a simple game of hangman.   It will loosely follow the strategy we used for the calculator in class. Follow these guidelines:

1. Write a (module level, not in the class) function mask that takes two string arguments, the first a word the second containing exceptions.   The function mask returns a string in which every character in the word has been replaced by ‘?’ except for the characters in exceptions. For example:

>>> mask('APPLE','AE')

'A???E'

>>> mask('APPLE','')

'?????'

>>> mask('APPLE','PLEASE')

'APPLE'

>>>

2. The interface:

a. hangman should inherit from Frame.

b. Hangman Label & Entry displays the current state of the word

c. Right Label & Entry – letters guessed correctly so far (no repeats listed)

d. Wrong Label & Entry – letters guessed incorrectly so far (repeats not listed)

e. 26 buttons, one for each letter of the alphabet

3. Gameplay. The player clicks on a letter button. Three things can happen:

a. the letter is already in either the list of right or wrong letters.In this case nothing happens.

b. the letter is in the word (but not in either list).Then that letter is added to the list of right letters, the word is remasked and the masked word (use mask) is displayed next to Hangman (in effect revealing the new letter).   If the word is finished a showinfo box states “You Win!”

c. the letter is not in the word (but not in either list).Then that letter is added to the list of wrong letters. If that list contains 6 or more letters, a showinfo box states “You Lose!”

4. Implementation details:

a. hangman subclasses Frame

b. you may assume that the user will not edit the Entry’s(this can be prevented but requires more than what we know now).

c. in addition to creating the interface, __init__ should take the given word, make it uppercase, and assign it to self.word

d. each button should set command=cmd, where cmd is a local function accepting one argument that defaults to the same letter on the button label (same thing we did with the calculator).   cmd calls another method click to implement the gameplay above.

Here is an example of how the game works.   First, start the game:

hangman('APPLE') # start with word ‘apple’

click ‘P’

click ‘P’ again

click ‘N’

‘L’

‘E’,then ‘A’

could be ‘You Lose’ if there are 6 wrong letters

click ‘P’

click ‘P’ again

click ‘N’

‘L’

‘E’,then ‘A’

could be ‘You Lose’ if there are 6 wrong letters

Explanation / Answer

module hangman
import random
HangmanDiagram = ['''
# Image Stage 1
     +----------+
      |              |
      |              |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
----------------------''', '''
# Image Stage 2

     +----------+
      |              |
      0             |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
----------------------''', '''

# Image Stage 3

     +----------+
      |              |
      0             |
      |              |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
----------------------''', '''

# Image Stage 4

     +----------+
      |              |
      0             |
    / |              |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
----------------------''', '''

# Image Stage 5

     +----------+
      |              |
      0             |
     / |             |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
----------------------''', '''

# Image Stage 6

     +----------+
      |              |
      0             |
     / |            |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
----------------------''', '''

# Image Stage 7

     +----------+
      |              |
      0             |
     / |            |
      /              |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
----------------------''', '''

# Image Stage 7+1

     +----------+
      |              |
      0             |
     / |            |
      /             |
                     |
                     |
                     |
                     |
                     |
                     |
                     |
----------------------''']
wordsFromDictionary = 'alpha beta baloon bag cat call case cave dare damn dog dawn dusk elephant egg eat employ '
Frame replaceLetter
def replaceLetter(singleWord):
   $ the function defined here will replace the selected letter or character with a question mark ?
   index = random.randint(0, len(singleWord) -1)
   singleWord[index] = '?'
   "".join(singleWord)
   return singleWord   #[index]
def findWinOrLose(character):
   # if the letter is not present in the word and also not in both of the lists
   # then showinfo box("You Lose!")
   #if the letter is present in the word but not in any of the lists,
   #then
   while True:
       print('Enter the index to be masked with a ? ')
       index = input()
       if len(input) != 1:
           print('Sorry, the index has to be a single digit')
       if input not in '0123456789':
           print('Oops, the index has to be a number between 0 to 9')
   return status # either Win or Lose