Skip to content

Commit db0bd07

Browse files
committed
Update code examples in Build a Word Guessing Game
1 parent 26c6831 commit db0bd07

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

projects/build-a-word-guessing-game-with-python/build-a-word-guessing-game-with-python.mdx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ We're almost done! We'll know if the player has won the game when they guess all
145145
break
146146
```
147147

148-
Finally, we need a condition that results in a loss for the player when they run out of `attempts` and reveal the correct word.
148+
Finally, we need a condition that results in a loss for the player when they run out of `attempts` and reveal the correct word. This means that this line of code needs to be written outside of the `while()` loop.
149149

150150
```py
151-
else:
152-
print('\nYou\'ve run out of attempts! The word was: ' + word)
151+
if attempts == 0 and '_' in guessedWord::
152+
print('\nYou\'ve run out of attempts! The word was: ' + word)
153153
```
154154

155155
**Note:** We don't need to break the `while` loop because the condition was `while attempts > 0`.
@@ -158,24 +158,24 @@ When you're finished, the game loop should look something like this:
158158

159159
```py
160160
while attempts > 0:
161-
162-
print('\nCurrent word: ' + ' '.join(guessedWord))
161+
print('\nCurrent word: ' + ' '.join(guessedWord))
163162

164-
guess = input('Guess a letter: ').lower()
163+
guess = input('Guess a letter: ').lower()
165164

166-
if guess in word:
167-
for i in range(len(word)):
168-
if word[i] == guess:
169-
guessedWord[i] = guess
170-
print('Great guess!')
171-
else:
172-
attempts -= 1
173-
print('Wrong guess! Attempts left: ' + str(attempts))
174-
if '_' not in guessedWord:
175-
print('\nCongratulations!! You guessed the word: ' + word)
176-
break
177-
else:
178-
print('\nYou\'ve run out of attempts! The word was: ' + word)
165+
if guess in word:
166+
for i in range(len(word)):
167+
if word[i] == guess:
168+
guessedWord[i] = guess
169+
print('Great guess!')
170+
else:
171+
attempts -= 1
172+
print('Wrong guess! Attempts left: ' + str(attempts))
173+
if '_' not in guessedWord:
174+
print('\nCongratulations!! You guessed the word: ' + word)
175+
break
176+
177+
if attempts == 0 and '_' in guessedWord:
178+
print('\nYou\'ve run out of attempts! The word was: ' + word)
179179
```
180180

181181
## Conclusion

0 commit comments

Comments
 (0)