{"id":434,"date":"2023-04-11T15:22:56","date_gmt":"2023-04-11T15:22:56","guid":{"rendered":"https:\/\/vmlogger.com\/algorithms\/?p=434"},"modified":"2023-04-11T15:22:56","modified_gmt":"2023-04-11T15:22:56","slug":"hangman-game-in-python-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/algorithms\/2023\/04\/11\/hangman-game-in-python-a-step-by-step-guide\/","title":{"rendered":"Hangman Game in Python – A Step-by-Step Guide"},"content":{"rendered":"
Have you ever played the hangman game before? It’s a classic word-guessing game that’s been around for decades. In this blog post, we’ll show you how to implement the hangman game in Python. We’ll go through the code and explain everything in detail, so even if you’re new to Python, you should be able to follow along.<\/p>\n
Firstly, I will show you how to generate a random word from the word list. We’ll use the random module to do this. In order to generate a random word, you need to have a list of words you want to use for the game. There can be two ways to get these words list: <\/p>\n
english-words 2.0.0<\/code><\/a><\/li>\n<\/ol>\n\r\nimport random\r\nfrom english_words import get_english_words_set\r\n\r\nwordsArray = list(get_english_words_set([\"web2\"], lower=True))\r\nselected_word = random.choice(wordsArray)\r\n<\/pre>\n\nInfo:<\/h3>\n
\n
get_english_words_set<\/code> method returns a set of English words. In order to randomly pick any word from this word list, I have used randon.choice()<\/code> method. This method requires a list<\/code> data type and therefore, I have typecasted the wordsArray<\/code> to list<\/code>.\n<\/div>\nHiding the letters of the chosen word<\/h2>\n
Now, we want to hide many of the letters from the word and replace them with underscore ( _ )<\/code>. This way player will be sure about the number of letters there in the given word.<\/p>\n\r\ndef hideCharacters(word):\r\n hidden_word = word\r\n num = random.randint(0, len(word) - 1)\r\n for i in range(0, len(word) - 1):\r\n if i != num:\r\n hidden_word = hidden_word.replace(word[i], \"_\")\r\n return hidden_word\r\n\r\n<\/pre>\nIn the above, function, I am randomly showing only one letter from the word, and the rest of the letters are replaced with an underscore<\/code>. In case you want to show more than 1 letter randomly, you can make changes in the above function. For example, the below function will show more letters for bigger words.<\/p>\n\r\ndef hideCharacters(word):\r\n hidden_word = word\r\n\r\n max_visible = len(word) \/\/ 4\r\n rand_places = []\r\n for i in range(max_visible):\r\n rand_places.append(random.randint(0, len(word) - 1))\r\n print(rand_places)\r\n for i in range(0, len(word) - 1):\r\n if i not in rand_places:\r\n hidden_word = hidden_word.replace(word[i], \"_\")\r\n return hidden_word\r\n<\/pre>\nHandling letter guessed by players and deciding winner:<\/h2>\n
Now we’ll handle user input. We’ll ask the player to guess a letter and check if it’s in the word. We will keep on updating the displayed word based on the player’s input. If the player guesses correctly, we’ll replace the underscores with the correct letters. Note: <\/code> If the letter guessed by the user is found at multiple places, then all the underscores will be replaced with that letter. <\/p>\nAlso if the letter was already guessed before, you do not lose the chance, rather player will be informed that the letter has already been guessed. Finally, we’ll check if the player has won or lost the game. We’ll keep track of the number of incorrect guesses and end the game if the player runs out of guesses or correctly guesses the word.<\/p>\n
\r\nimport random\r\nimport re\r\nfrom words import word_list\r\nfrom english_words import get_english_words_set\r\n\r\n\r\ndef hideCharacters(word):\r\n hidden_word = word\r\n max_visible = len(word) \/\/ 4\r\n rand_places = []\r\n for i in range(max_visible):\r\n rand_places.append(random.randint(0, len(word) - 1))\r\n print(rand_places)\r\n for i in range(0, len(word) - 1):\r\n if i not in rand_places:\r\n hidden_word = hidden_word.replace(word[i], \"_\")\r\n return hidden_word\r\n\r\n\r\ndef startTheGame():\r\n print(\"Hello welcome to the new game... \")\r\n gamer = input(\"Enter your Name: \")\r\n print(\"Hello \" + gamer + \"! Welcome to hangman... Good luck!!\")\r\n print(\"Let's begin...\")\r\n print(\"************************\")\r\n nextRound()\r\n\r\n\r\ndef nextRound():\r\n guesses = 6\r\n wordsArray = list(get_english_words_set([\"web2\"], lower=True))\r\n # selected_word = random.choice(word_list)\r\n selected_word = random.choice(wordsArray)\r\n print(\"Total Chances : 6\")\r\n print(\"Here you go with your word...\")\r\n hidden_word = hideCharacters(selected_word)\r\n print(hidden_word)\r\n startGuessing(guesses, hidden_word, selected_word)\r\n\r\n\r\ndef startGuessing(guesses, hidden_word, selected_word):\r\n guessed_letters = []\r\n guessed = False\r\n while not guessed and guesses > 0:\r\n l = input(\"Your next guess : \")\r\n if l in guessed_letters:\r\n print(\"Letter is already guessed !\")\r\n elif l in selected_word:\r\n occurances = [pos.start() for pos in re.finditer(l, selected_word)]\r\n for i in occurances:\r\n hidden_word = hidden_word[:i] + l + hidden_word[i + 1 :]\r\n print(hidden_word)\r\n else:\r\n guesses = guesses - 1\r\n print(\"Wrong guess. Chances Left: \" + str(guesses))\r\n guessed_letters.append(l)\r\n if hidden_word == selected_word:\r\n guessed = True\r\n\r\n if hidden_word != selected_word:\r\n print(\"The word was : \" + selected_word)\r\n print(\"Sorry !! You lost this round !\")\r\n if input(\"Do you want to know the word? (y\/n): \").upper() == \"Y\":\r\n print(\"The Word was: \" + selected_word)\r\n\r\n else:\r\n print(\"Congratulations!! You won!!\")\r\n\r\n\r\ndef main():\r\n startTheGame()\r\n while input(\"Want to play again? (y\/n) : \").upper() == \"Y\":\r\n nextRound()\r\n print(\"Thank you!! See you next time...\")\r\n\r\n\r\nif __name__ == \"__main__\":\r\n main()\r\n\r\n<\/pre>\nConclusion:<\/h2>\n
Congratulations! You’ve successfully implemented the hangman game in Python. We hope you found this tutorial helpful and enjoyed playing the game. Now that you have the basic hangman game code, you can customize it and make it your own. Happy coding!
\nYou can find this entire code in github repository<\/a> too. <\/p>\n<\/span>","protected":false},"excerpt":{"rendered":"Have you ever played the hangman game before? It’s a classic word-guessing game that’s been around for decades. In this blog post, we’ll show you how to implement the hangman game in Python. We’ll go through the code and explain everything in detail, so even if you’re new to Python, you should be able to […]<\/p>\n","protected":false},"author":45,"featured_media":450,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[3],"tags":[],"class_list":["post-434","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-easy"],"yoast_head":"\n
Hangman Game in Python - A Step-by-Step Guide - Algorithms<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n\n\n\n\t\n\t\n\t\n