Today's, cough, task was to create the hangman game.
The lessons build the game slowly. It starts with designing a flowchart (See the README.md on my GitHub repo,) and after I wrote that and switched up a gear when I hit a marker on the flow chart for the first part of the code, I skimmed through the other lessons.
For some reason, unlike day 6, this just seemed to click.
For the list of words, I use two ways to grab the word to be guessed.
- random.shuffle, to mix up the word list
- random.randint(0,len(word_list)-1), to use a random number to choose from the shuffled list.
Here is my initial code. I grabbed some of the words from Geeks For Geeks to save me having to think about the words.
from random import randint, shuffle
from os import system
# Generate the Random word
system('cls')
word_list = ['aardvark', 'baboon', 'camel', 'rainbow', 'computer', 'science', 'programming', 'python', 'mathematics', 'player', 'condition', 'reverse', 'water', 'board', 'geeks']
shuffle(word_list) # Shuffle the word list
x = randint(0,len(word_list)-1)
# Get User Guess
# guess = input('Guess a letter that may be in the word: \n').lower()
guess = 'a'
the_word = word_list[x] # Grab a word from the shuffled list
if guess in the_word:
print('The letter is in the word')
else:
print('This letter is not in the word')
# system('cls')
# print(f'Your guess was {guess}')
print(f'The word letters were {the_word}')
From there on it was all about making each part work.
Fixing the word choice and the guess were essential to testing the process of showing the blank letter count for the word choice, the hangman noose, checking guesses, showing the correct guesses, the wrong guesses and set a counter to limit the guesses to match the hangman picture.
I used a list, blank_string, to hold a list of underline for each letter of the chosen word.
Converted the word choice into a list, the_word_list, and with a while loop did a comparison between them. This gave the loop for continual guessing.
Next the users guess is compared to the_word_list for correctness., if it is there then, and using the positional, the underline in blank_string was changed to the correct letter.
If it was an incorrect comparison, the number of tries was incremented, the hangman's noose was increased using the number of tries to get the appropriate hangman's noose ascii from the list of pictures.
If the user ran out of guesses, they would lose and if they got every letter they would win.
After that, it was all about tidying up the look in the console. Clearing the console at the appropriate points and presenting the blank_string with correct answers shown when appropriate and showing various other points along the way.
One point that I don't know if it was used in the lessons or not was with the ascii hangman's noose. I got a copy of this art from here on GitHub. I put this into a separate module and imported it. It was in the form of a list that was easy to use, the tries counter as the positional selected the correct hangman's noose to print to console.
I really enjoyed getting to grips with this code tonight, and look forward tomorrow's challenge what every that is. I won't look until the morning.
If you want to see the final code you should look in my GitHub repo
The course I am following is HERE