-
Notifications
You must be signed in to change notification settings - Fork 57
/
hangman part 2 the game (both boxes).py
58 lines (46 loc) · 2.18 KB
/
hangman part 2 the game (both boxes).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def hangman(secretWord):
'''
secretWord: string, the secret word to guess.
Starts up an interactive game of Hangman.
* At the start of the game, let the user know how many
letters the secretWord contains.
* Ask the user to supply one guess (i.e. letter) per round.
* The user should receive feedback immediately after each guess
about whether their guess appears in the computer's word.
* After each round, you should also display to the user the
partially guessed word so far, as well as letters that the
user has not yet guessed.
Follows the other limitations detailed in the problem write-up.
'''
# FILL IN YOUR CODE HERE...
intro = str(len(secretWord))
lettersGuessed = []
guess = str
mistakesMade = 0
print 'Welcome to the game, Hangman!'
print ('I am thinking of a word that is ') + intro + (' letters long.')
print ('------------')
while mistakesMade < 8:
if isWordGuessed(secretWord,lettersGuessed):
return ('Congratulations, you won!')
print ('You have ') + str(8-mistakesMade) + (' guesses left.')
print ('Available letters:') + getAvailableLetters(lettersGuessed)
guess = raw_input('Please guess a letter:').lower()
if guess in secretWord:
if guess in lettersGuessed:
print ('Oops! You\'ve already guessed that letter:') + getGuessedWord(secretWord, lettersGuessed)
print ('------------')
else:
lettersGuessed.append(guess)
print ('Good guess:') + getGuessedWord(secretWord, lettersGuessed)
print ('------------')
else:
if guess in lettersGuessed:
print ('Oops! You\'ve already guessed that letter:') + getGuessedWord(secretWord, lettersGuessed)
print ('------------')
else:
lettersGuessed.append(guess)
mistakesMade += 1
print ('Oops! That letter is not in my word:') + getGuessedWord(secretWord, lettersGuessed)
print ('------------')
return ('Sorry, you ran out of guesses. The word was ') + secretWord