WordPress.com
[pic]
Pyhton Assignment-2 HANGMAN GANE- Learn Spelling
1. Player One picks a secret word and draws a line for each letter in it (you will use an underscore to represent each line).
2. Player Two tries to guess the word one letter at a time.
3. If Player Two guesses a letter correctly, Player One replaces the corresponding underscore with the correct letter. In this version of the game, if a letter appears twice in a word, you have to guess it twice.
OR
If Player Two guesses incorrectly, Player One draws a body part of a hanged stick figure (starting with the head).
4. If Player Two completes the word before the drawing of the hangman is complete, they win. If not, they lose.
Step to Perform:-
1. First, you create a function called hangman to store the game.
2. The function accepts a variable called word as a parameter; this is the word Player Two has to guess.
def hangman(word):
3. You use another variable, wrong, to keep track of how many incorrect letters Player Two has guessed.
wrong = 0
4. The variable stages is a list filled with strings you will use to draw your hangman. When Python prints each string in the stages list on a new line, a picture of a hangman forms.
stages = ["",
"________ ",
"| ",
"| | ",
"| 0 ",
"| /|\ ",
"| / \ ",
"| "
]
5. The variable rletters is a list containing each character in the variable word that keeps track of which letters are left to guess.
rletters = list(word)
6. The variable board is a list of strings used to keep track of the hints you display to Player Two, e.g., c__t if the word is cat (and Player Two has already correctly guessed c and t). You use [“__”] * len(word) to populate the board list, with an underscore for every character in the variable word. For example, if the word is cat, board starts as [“__”, “__”, “__”].
board = ["__"] * len(word)
7. You also have a win variable that starts as False, to keep track of whether Player Two has won the game yet. Next, you print Welcome to Hangman.
win = False
print("Welcome to Hangman")
8. Once you are inside your loop, print a blank space to make the game look nice when it prints in the shell. Then, collect Player Two’s guess with the built-in input function and store the value in the variable guess.
9. If guess is in rletters (the list that keeps track of the letters in the word that Player Two hasn’t guessed yet), the player guessed correctly. If the player guessed correctly, you need to update your board list, which you use later in the game to display the letters remaining. If Player Two guessed c, you would change your board list to [“c”, “__”, “__”].
10. To do this, you use the index method on your rletters list to get the first index of the letter Player Two guessed, and use it to replace the underscore in board at the index with the correctly guessed letter.
while wrong < len(stages) - 1:
print("\n")
msg = "Guess a letter"
char = input(msg)
if char in rletters:
cind = rletters \
.index(char)
11. There is one problem with this. Because index only returns the first index of the character you are looking for, your code will not work if the variable word has more than one of the same character. To get around this, modify rletters by replacing the character that was correctly guessed with a dollar sign, so the next time around the loop, the index function will find the next occurrence of the letter (if there is one) and it won’t stop at the first occurrence.
12. If on the other hand, if the player guesses an incorrect letter, you increment wrong by 1.
13. Next, you print the scoreboard and your hangman using the board and stages lists. The code that prints the scoreboard is ‘ ‘.join(board).
board[cind] = char
rletters[cind] = '$'
else:
wrong += 1
print((" ".join(board)))
e = wrong + 1
14. Printing the hangman is trickier. When each of the strings in your stages list prints on a new line, a complete picture of a hangman prints. You can create the entire hangman by printing ‘\n’.join(stages), which adds a new line to each string in the stages list so that each string in the list prints on a separate line.
print("\n"
.join(stages[0: e]))
15. To print your hangman at whatever stage the game is at, you slice your stages list. You start at stage 0, and slice up to the stage you are at (represented by the variable wrong) plus one.
16. You add one because when you are slicing, the end slice does not get included in the result. This slice gives you only the strings you need to print the version of the hangman you are currently at.
17. Finally, you check if Player Two won the game. If there are no more underscores in the board list, they guessed all the letters and won the game. If Player Two won, you print You win! It was: and the word they correctly guessed. You also set the variable win to True, which breaks you out of your loop.
18. Once you break out of your loop, if Player Two won, you do nothing—the program is over. If they lost, the variable win is False. If that is the case, you print the full hangman and You lose!, followed by the word they couldn’t guess:
if "__" not in board:
print("You win!")
print(" ".join(board))
win = True
break
if not win:
print("\n"
.join(stages[0: \
wrong]))
print("You lose! It was {}."
.format(word))
hangman("cat")
Output- 1 if guessing character is correct in each stages
[pic]
Output- 1 if guessing character is not correct in each stages it start draw hangman
Guess a letter b
__ __ __
________
Guess a letter d
__ __ __
________
|
Guess a letter e
__ __ __
________
|
| |
Guess a letter f
__ __ __
________
|
| |
| 0
Guess a letter c
c __ __
________
|
| |
| 0
Guess a letter al
c __ __
________
|
| |
| 0
| /|\
Guess a letter k
c __ __
________
|
| |
| 0
| /|\
| / \
Guess a letter f
c __ __
________
|
| |
| 0
| /|\
| / \
|
________
|
| |
| 0
| /|\
| / \
You lose! It was cat.
*********** THE END*************************
Complete Code-
def hangman(word):
wrong = 0
stages = ["",
"________ ",
"| ",
"| | ",
"| 0 ",
"| /|\ ",
"| / \ ",
"| "
]
rletters = list(word)
board = ["__"] * len(word)
win = False
print("Welcome to Hangman")
while wrong < len(stages) - 1:
print("\n")
msg = "Guess a letter"
char = input(msg)
if char in rletters:
cind = rletters \
.index(char)
board[cind] = char
rletters[cind] = '$'
else:
wrong += 1
print((" ".join(board)))
e = wrong + 1
print("\n"
.join(stages[0: e]))
if "__" not in board:
print("You win!")
print(" ".join(board))
win = True
break
if not win:
print("\n"
.join(stages[0: \
wrong]))
print("You lose! It was {}."
.format(word))
hangman("cat")
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- assumption university
- assignment no
- python for kids lesson plan no starch
- pandas groupby in action edu
- python class room diary be easy in my python class
- university of pittsburgh
- answers to chapters 1 2 3 4 5 6 7 8 9 end of chapter
- introduction amazon web services
- informatics practices new 065 class xii
- math framework chapter 5 curriculum frameworks ca dept
Related searches
- wordpress passing data between pages
- wordpress business templates
- wordpress rss feed not working
- wordpress jquery is not defined
- create wordpress blog
- wordpress roles editor
- wordpress full rss feed
- wordpress rss feed settings
- wordpress rss feed plugin
- wordpress display rss feed
- wordpress rss feed link
- wordpress rss feed to post