Skip to content

A game of hangman to practise basic python syntax

Notifications You must be signed in to change notification settings

amalsebastian7/HangMan

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Hangman

A game of hangman to practise basic python syntax

Description

To start, the program chooses a word at random from the word list, and then displays the number of unique characters and the gapped word. E.g.

	The mystery word has 9 characters.
	['_', '_', '_', '_', '_', '_', '_', '_', '_', '_']

The user is asked for a letter and the program checks if it is in the word. If it is, its position is displayed in the gapped word and the game continues. If it is not, the player loses a life, and the next stage of the hangman diagram is drawn.

This continues until the player either guesses the word, and therefore wins, or runs out of lives, in which case, they lose.

This project makes use of Object Oriented Programming. A Hangman class is defined with the following attributes and methods:

•	attributes:
  ⁃	word: str. The word to be guessed picked randomly from the word_list.
  ⁃	word_guessed: list. A list of the letters of the word, with '_' for each letter not yet guessed. For example, if the word is 'apple', the word_guessed list would be ['_', '_', '_', '_', ‘_’]. If the player guesses 'a', the list would be ['a', '_', '_', '_', ‘_’].
  ⁃	num_letters: int. The number of unique letters in the word that have not been guessed yet.
  ⁃	num_lives: int. The number of lives the player has.
  ⁃	list_letters: list. A list of the letters that have already been tried.
  ⁃	diagram: list. A list containing the stages of the hangman diagram.
•	methods:
  ⁃	check_letter(letter). This checks if the letter is in the word.
  ⁃	ask_letter(). This asks the user for a letter, and checks that it is valid, i.e. is only one character and has not been used before.

In the play_game() function, the class is called to create an instance of the game, which the user then interacts with.

Milestone 1: This stage involved coding the ask_letter() method. Using a while loop, it asks the user for a letter, assigning it to the variable, letter. With an if-elif-else statement, it then checks that the letter meets the criteria of being a single character. If it does, the while loop breaks. If it doesn’t, the user is asked again for a single character.

Milestone 2: Here, using the init constructor, I initialised the attributes as described above. I also included 2 print statements to appear when the game starts, i.e. when an instance of the class is initialised.

Now that I had the list_letter attribute, I could amend the ask_letter() method so that it also checks that the letter hasn’t been asked previously.

Milestone 3: Here, I was coding the check_letter() method. This method needed to check whether the letter is in the word. If it is, it replaces the “_” in the word_guessed list with the letter. If it is not, it reduces the number of lives by 1. In both cases, the letter needed to be added to the list_letters in case the user tried to guess this letter again.

This method starts by converting letter to lower case, so it can handle any input from the user. It then runs through an if-else statement. If the letter is in the word, the program:

1. finds the index of that letter in the word, 
2. then deletes the “_” at that index in the word_guessed,
3. before inserting the letter to that index. 

Milestone 4: The last thing to do was complete the play_game() function.

Bonus task: I wanted to include the diagram that appears in the traditional game of Hangman. So, I added in another attribute to the class, called diagram. This is a list of the different stages of the drawing.

1. Installations:

No special libraries required. The standard Python installation.

2. Project Motivation:

The project was created for the AiCore bootcamp programme to give me an opportunity to practice my Python skills and make sure I am at the level to progress through the programme.

3. File Descriptions:

hangman/hangman_solution.py - script with TODOs from template completed

4. How to interact with the project:

The project can be viewed as a stand alone demonstration of a simple Python project and you can download the completed version to play the game.

5. Licensing, Authors, Acknowledgements:

The template was generated by IvanYingX and was accessed via the AiCore programme.

The author of the work is Amal Sebastian.

About

A game of hangman to practise basic python syntax

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages