100 Days of code (python course by angela) project 8
"Mastering the Art of Hangman: Creating a Fun Game with Python's List, While Loop and For Loop"
Hangman is a classic word-guessing game that has been enjoyed by people of all ages for generations. The game is simple: the player guesses letters to try to figure out a hidden word. However, creating a hangman game with Python can be a challenging task, especially if you're a beginner. In this post, we will be taking a look at how to create a hangman game using Python's list, while loop, and for loop. We will walk you through step-by-step instructions on how to build your own version of the game, including how to select a random word from a list, how to prompt the user for their guess, and how to track their progress. By the end of this post, you will have mastered the art of hangman and be able to create a fun and engaging game for yourself and others to enjoy!
1. Introduction to the game of Hangman
Hangman is an age-old game that has been enjoyed by many generations. It is a word-guessing game where one player thinks of a word and the other player tries to guess it by suggesting letters. Each incorrect guess results in the drawing of a stick figure being hanged, and the player has to guess the word before the stick figure is completely drawn.
In a digital age, Hangman is still popular and can be created with the help of Python's List, While Loop, and For Loop. It is a great way to learn programming and improve your problem-solving skills.
The game has simple rules, yet it can be challenging and engaging. It is a perfect game for kids and adults alike. The possibilities for word choices are endless, making the game fun and diverse. In this tutorial, we will guide you through the process of creating your own Hangman game using Python. You will learn how to create lists, loops, and conditional statements to bring your game to life. So, let's get started and have some fun!
2. Basic components of the game and how they work
Hangman is a simple yet entertaining word game that can be created using Python's list, while loop, and for loop. To play the game, one person thinks of a word and the other person tries to guess the word by suggesting letters one at a time. For each incorrect guess, a part of a "hangman" is drawn. If the entire hangman is drawn before the word is guessed, the game is lost. Otherwise, the guesser wins the game by successfully guessing the word.
The basic components of the game include a list of words, a variable to track the number of incorrect guesses, a while loop to keep the game running until the word is guessed or the hangman is drawn, and a for loop to check each guess against the letters in the word.
To begin the game, a word is chosen from the list of words and the hangman is displayed as a series of underscores. The guesser then inputs a letter, which is checked against the letters in the word using a for loop. If the guess is correct, the corresponding underscore is replaced with the letter in the correct position. If the guess is incorrect, the number of incorrect guesses is incremented and a new part of the hangman is drawn.
The game continues until the word is guessed or the hangman is complete. Once the game is over, the player is given the option to play again or exit the game. With a few lines of code, Hangman can be created using Python's list, while loop, and for loop, providing hours of fun for players of all ages.
Source code:
#main file //////////
import random
from hangman_art import stages
import clear
from hangman_words import word_list
chosen_word = random.choice(word_list)
word_length = len(chosen_word)
end_of_game = False
lives = 6
from hangman_art import logo
print(logo)
print("The word is " + chosen_word)
display = []
for _ in range(word_length):
display += "_"
while not end_of_game:
guess = input("Guess a letter: ").lower()
clear.clear ()
# - If the user has entered a letter they've already guessed, print the letter and let them know.
if guess in display:
print(f"You've already guessed {guess}")
#Check guessed letter
for position in range(word_length):
letter = chosen_word[position]
#print(f"Current position: {position}\n Current letter: {letter}\n Guessed letter: {guess}")
if letter == guess:
display[position] = letter
# - If the letter is not in the chosen_word, print out the letter and let them know it's not in the word.
if guess not in chosen_word:
print(f"You guessed {guess}, that's not in the word. You lose a life.")
print(stages[lives])
lives -= 1
if lives == 0:
end_of_game = True
print("You lose.")
print(f"{' '.join(display)}")
#Check if user has got all letters.
if "_" not in display:
end_of_game = True
print("You win.")
#hangman_art file //////////////
stages = ['''
+---+
| |
O |
/|\ |
/ \ |
|
=========
''', '''
+---+
| |
O |
/|\ |
/ |
|
=========
''', '''
+---+
| |
O |
/|\ |
|
|
=========
''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========
''', '''
+---+
| |
O |
|
|
|
=========
''', '''
+---+
| |
|
|
|
|
=========
''']
logo = '''
_
| |
| |__ __ _ _ __ __ _ _ __ ___ __ _ _ __
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|___/ '''
#hangman-words file //////////////////
word_list = [
'abruptly',
'absurd',
'abyss',
'affix',
'askew',
'avenue',
'awkward',
'axiom',
'azure',
'bagpipes',
'bandwagon',
'banjo',
'bayou',
'beekeeper',
'bikini',
'blitz',
'blizzard',
'boggle',
'bookworm',
'boxcar',
'boxful',
'buckaroo',
'buffalo',
'buffoon',
'buxom',
'buzzard',
'buzzing',
'buzzwords',
'caliph',
'cobweb',
'cockiness',
'croquet',
'crypt',
'curacao',
'cycle',
'daiquiri',
'dirndl',
'disavow',
'dizzying',
'duplex',
'dwarves',
'embezzle',
'equip',
'espionage',
'euouae',
'exodus',
'faking',
'fishhook',
'fixable',
'fjord',
'flapjack',
'flopping',
'fluffiness',
'flyby',
'foxglove',
'frazzled',
'frizzled',
'fuchsia',
'funny',
'gabby',
'galaxy',
'galvanize',
'gazebo',
'giaour',
'gizmo',
'glowworm',
'glyph',
'gnarly',
'gnostic',
'gossip',
'grogginess',
'haiku',
'haphazard',
'hyphen',
'iatrogenic',
'icebox',
'injury',
'ivory',
'ivy',
'jackpot',
'jaundice',
'jawbreaker',
'jaywalk',
'jazziest',
'jazzy',
'jelly',
'jigsaw',
'jinx',
'jiujitsu',
'jockey',
'jogging',
'joking',
'jovial',
'joyful',
'juicy',
'jukebox',
'jumbo',
'kayak',
'kazoo',
'keyhole',
'khaki',
'kilobyte',
'kiosk',
'kitsch',
'kiwifruit',
'klutz',
'knapsack',
'larynx',
'lengths',
'lucky',
'luxury',
'lymph',
'marquis',
'matrix',
'megahertz',
'microwave',
'mnemonic',
'mystify',
'naphtha',
'nightclub',
'nowadays',
'numbskull',
'nymph',
'onyx',
'ovary',
'oxidize',
'oxygen',
'pajama',
'peekaboo',
'phlegm',
'pixel',
'pizazz',
'pneumonia',
'polka',
'pshaw',
'psyche',
'puppy',
'puzzling',
'quartz',
'queue',
'quips',
'quixotic',
'quiz',
'quizzes',
'quorum',
'razzmatazz',
'rhubarb',
'rhythm',
'rickshaw',
'schnapps',
'scratch',
'shiv',
'snazzy',
'sphinx',
'spritz',
'squawk',
'staff',
'strength',
'strengths',
'stretch',
'stronghold',
'stymied',
'subway',
'swivel',
'syndrome',
'thriftless',
'thumbscrew',
'topaz',
'transcript',
'transgress',
'transplant',
'triphthong',
'twelfth',
'twelfths',
'unknown',
'unworthy',
'unzip',
'uptown',
'vaporize',
'vixen',
'vodka',
'voodoo',
'vortex',
'voyeurism',
'walkway',
'waltz',
'wave',
'wavy',
'waxy',
'wellspring',
'wheezy',
'whiskey',
'whizzing',
'whomever',
'wimpy',
'witchcraft',
'wizard',
'woozy',
'wristwatch',
'wyvern',
'xylophone',
'yachtsman',
'yippee',
'yoked',
'youthful',
'yummy',
'zephyr',
'zigzag',
'zigzagging',
'zilch',
'zipper',
'zodiac',
'zombie',
]
In conclusion, creating a Hangman game using Python's list, while loop, and for loop is a great way to improve your programming skills. By following the step-by-step guide in this article, you should now have a good understanding of how to create a functional and entertaining game.
But this is just the beginning. There are many ways you can improve and customize the game to make it even more fun and engaging. You can add more features such as multiple word categories, hints, and even a scoring system. You can also experiment with different graphics, sound effects, and user interfaces to enhance the overall experience.
To continue your learning journey, you can explore more advanced Python concepts such as object-oriented programming, file handling, and GUI development. You can also check out other game development tutorials and resources to expand your knowledge and skills.
Remember, mastering the art of Hangman is not just about creating a game, but also about honing your problem-solving, logic, and creativity skills. So, keep practicing, experimenting, and having fun with Python!
We hope you enjoyed our tutorial on how to create a fun game of Hangman using Python's List, While Loop, and For Loop. By following the step-by-step instructions provided in this article, you can take your Python programming skills to the next level and create a classic game that's both interactive and engaging. Whether you're a beginner or an experienced programmer, the tips and tricks shared in this post will prove invaluable. We can't wait to see what other fun projects you create using Python!