DAY 1 of 21DaysofBuild
DAY 1 of 21DaysofBuild
SJ
In this project, we'll use:
The random module
A code editor like VS Code or other of your
choice.
import random
Next, let's create a list of words that the game can
use as possible answers. For simplicity, we'll use five
words, but you can add as many as you'd like!
python
word = random.choice(word_bank)
python
attempts = 10
Save the file, and let's jump into building the game
loop!
##Game Loop
We need to create a while loop so the player can
continuously guess until they run out of attempts or
guess the word correctly. 🔁
python
python
python
if guess in word:
for i in range(len(word)):
if word[i] == guess:
guessedWord[i] = guess
print('Great guess!')
python
else:
attempts -= 1
print('Wrong guess! Attempts left: ' +
str(attempts))
python
else:
print('\nYou\'ve run out of attempts! The word
was: ' + word)
python
if guess in word:
for i in range(len(word)):
if word[i] == guess:
guessedWord[i] = guess
print('Great guess!')
else:
attempts -= 1
print('Wrong guess! Attempts left: ' +
str(attempts))
if '_' not in guessedWord:
print('\nCongratulations!! You guessed the
word: ' + word)
break
else:
print('\nYou\'ve run out of attempts! The
word was: ' + word)
# Conclusion
Congratulations! You have completed the project!
🎉🎊
In this tutorial, you used the following to build a
word-guessing game: