Introduction: Py Word Game V1.1

About: Passionate about Electronics and Communication Engineering, IOT, Microcontrollers.

Hello!!

In this Instructable, I am going to share a simple word game made using python code.

To start with I am new to python coding with basic knowledge in C. This game is pretty easy to make with only basic level of coding required!!

Let me explain about the game:

1) The Computer generates a random word from a given list of English words from a "Dictionary"

2) the player has to guess a word from the last letter of the given word

3) the computer takes the user input and generates another word from the "Dictionary" which start with the last letter of player word.

like this the game continues.

The game is over when :

1) the player fail to guess word which starts with the last letter of given word.

2) guess the word which is not there in the "Dictionary"

Note : if the user input word is already guessed previously, then another chance is given to user stating that the word is already guessed.

Materials Required:

1) IDE to develop python code : Any IDE can be use. I Use Anaconda Navigator which provide Spyder Python IDE.
Anaconda can be downloaded here : https://www.anaconda.com/products/individual

2) "Dictionary" : Dictionary is a text file which contains list of English permitted to be used in the game. Many such word file can be found in GitHub. One such file is used here.

GitHub link for Dictionary : https://github.com/dwyl/english-words

The file words_alpha is used which contain only words in seperate lines.

Note: The Dictionary file should be kept in the same folder of the python file containing the code

Step 1: Creating a List of Words Present in the Dictionary and Staring the Game!

The first step in the process is to open the file and read the words and store it in a list.

for opening the file open() function can be used.

fo = open("words_alpha.txt","r")

this means open the text file words_alpha.txt in read mode.

After opening the file, a list containing all the words needs to be created by using readlines()

word_list = fo.readlines()

Note : here in the list the last letter of each element is '\n' because the file is new-line-delimited.

Hence the list is created. Then we need to pick a random word from this list for the user to start the game.

This is done by :

current_word = random.choice(word_list)

note: import random library from python.

Then we will append this to a list of words already guessed by using append()

guessed_words.append(current_word)

Step 2: Input the Player Word and Check Its Validity.

the player word is taken as input using input() and following things are checked:

1) if the word is already guessed : give another chance .

2) is the word starts with the ending letter of the computer word : if not, end the game the player loses

3) is the word present in dictionary : is not end the game.

if the Word is valid then append both the user word and computer word into list of guessed words.

Note : the negative index refers to the letter position from the right ( end ) of the word.

Hence [-1] means the last element, [-2] is the second from last and so on.

Note :in step one its noted that the words list has '/n' as last element [-1], hence index [-2] is used to find last letter.

Step 3: Finding a Random Word Which Starts With Last Letter of Player Word

For this Step while loop is used to make a list of words which begins with last letter of player word.

for word in word_list:
if(word[0] == player_word[-1]): word_list_begin_char.append(word)

Then a random word is chosen. If the random word is already guessed, then another random word is chosen till the word is valid.

while True:
computer_word = random.choice(word_list_begin_char)

if computer_word not in guessed_words : break


Step 4: Ending the Game With Proper Message

As explained the Game will be over if the certain conditions are not met.

hence "Game Over! " message is displayed at the end.

the player gets a option to restart a new game or to quit.

Step 5: Finishing and Playing the Game!!

All the steps are done!!

Only thing left to play the game.

Save the file with .py extension.

Place the file in the same folder as the Dictionary File.

Double click the .py file to play the game.

The game is pretty basic with CMD interface.

Step 6: Next Process??

This is a Basic Game (V1.1), which involves only Player Vs Computer.

Many Improvements can be made to this like (PvP etc) .

Please Comment down any improvements needed!

The Code is pretty basic and may not be optimum. Please suggest any optimization if needed.

Hope you enjoyed the process!!!

Have Fun!!