0% found this document useful (0 votes)
27 views

DAY 1 of 21DaysofBuild

Uploaded by

sarthakjain1024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

DAY 1 of 21DaysofBuild

Uploaded by

sarthakjain1024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Day 1

SJ
In this project, we'll use:
The random module
A code editor like VS Code or other of your
choice.

#Create the Word Bank


First, we'll need to create a word bank of all the
possible words to be guessed, and randomly select
one to be the correct answer. This way, the game
has an element of mystery and surprise! 🔍
Start by importing the random module:
python

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_bank = ['rizz', 'ohio', 'sigma', 'tiktok',


'skibidi']

We'll then randomly pick one of these words to be


the correct answer. 🎲
python

word = random.choice(word_bank)

The random.choice() method is used to randomly


select a word from the word_bank list and to assign
it to the word variable. All possible options have an
equal probability of being chosen.

#Finishing the Setup


To start, we won't show the letters in the word.
Instead, we'll use underscores as placeholders to
represent each letter that hasn't been guessed yet.
The number of underscores should match the
length of the word (e.g., "apple" would have five
underscores).
python

guessedWord = ['_'] * len(word)

In Python, you can multiply a list by an integer and


the list will repeat that amount of times. The ['_']
creates a list with a single underscore element,
while the len(word) function returns the length (or
number of characters) in the word string.

Next, we'll decide how many attempts a player has


to guess the word. For this tutorial, we'll use 10
attempts, but you can set any number you like.

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

while attempts > 0:

Each time the loop runs, we will display the current


state of the word being guessed.

python

while attempts > 0:


print('\nCurrent word: ' + ' '.join(guessedWord))

The statement is printed on a new line via \n, and


joins the strings in guessedWord together with
spaces. For example, if the state of guessedWord is
['a', '_', 'p', '_', 'e'], the result would be the string
"a _ p _ e", making the program look smoother and
less clunky.

Next, we'll ask the player to guess a letter and store


their input.

python

guess = input('Guess a letter: ')

Now, we'll check if the guessed letter is in the word.


If it is, we'll find its position(s) in the word and
replace the placeholders with the guessed letter.
python

if guess in word:
for i in range(len(word)):
if word[i] == guess:
guessedWord[i] = guess
print('Great guess!')

The for loop goes through each index of the word to


check every letter. If the player's guess matches a
letter, we update guessedWord at that position. We
also print positive feedback.

If the guess isn't in the word, we'll reduce the


number of attempts by one and inform the player
of how many attempts remain.

python

else:
attempts -= 1
print('Wrong guess! Attempts left: ' +
str(attempts))

To win, the player must guess all letters correctly.


When there are no more underscores in
guessedWord, the player has won, and we can
break the loop.
python

if '_' not in guessedWord:


print('\nCongratulations!! You guessed the
word: ' + word)
break

Finally, we need a condition that results in a loss for


the player when they run out of attempts and
reveal the correct word.

python

else:
print('\nYou\'ve run out of attempts! The word
was: ' + word)

Note: We don't need to break the while loop


because the condition was while attempts > 0.
When you're finished, the game loop should look
something like this:

python

while attempts > 0:

print('\nCurrent word: ' + '


'.join(guessedWord))

guess = input('Guess a letter: ').lower()

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:

The random module


List and string operations
Input/output handling
Control flow statements

Now, you can enjoy this game you built yourself, or


with friends!

If you would like to challenge yourself, try to add a


"hangman" stick figure that is changed every time
the user guesses a character incorrectly.

## Don’t Forget to hit the follow button

You might also like