Hangman Game
More on lists:
What is the output from the next code snippet?
>>> wordIndex = 2 >>> print(wordIndex) 2 >>> print(['biology','chemistry','physics','algebra'][wordIndex]) physics >>>
Displaying the Board to the Player
def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord): print(HANGMANPICS[len(missedLetters)]) print() print('Missed letters:', end=' ') for letter in missedLetters: print(letter, end=' ') print()
blanks = '_' * len(secretWord) for i in range(len(secretWord)): # replace blanks with correctly guessed letters if secretWord[i] in correctLetters: blanks = blanks[:i] + secretWord[i] + blanks[i+1:] for letter in blanks: # show the secret word with spaces in between each letter print(letter, end=' ') print()
Open a new window and create a python program, YI_displayBoard.py and copy the following code:
secretWord = 'apple' blanks = '_' * len(secretWord) correctLetters = 'l' blanks for i in range(len(secretWord)): # replace blanks with correctly guessed letters if secretWord[i] in correctLetters: blanks = blanks[:i] + secretWord[i] + blanks[i+1:] for letter in blanks: # show the secret word with spaces in between each letter print(letter, end=' ') print()
Change the correctLetters variable and play with the program until you figure out how it works.
There is a post in edmodo.com for you to submit the file and to explain how the program works.
Homework:
Write YI_WheelOfFortune.py using what you learned from Hangman.py
1. Have the variable secretWord changed to secretPhrase and have a value assigned to it.
2. Prompt the user for a letter and display it in the right positions if it is in the secretPhrase. Otherwise, send a message to the player “incorrect” and keep track of how many times the player guesses the wrong letters.
3. Terminate the game after 5 tries. Display “game over” if it fails. Otherwise, displays “You are a winner!!” and add message with the prize(s)