{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## `Q10.`##\n", "\n", "## `Devise a Python program to implement the Hangman Game.`##" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## -----------------------------------------------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**`Hangman`** is a paper and pencil guessing game for two or more players. One player thinks of a word, phrase or sentence and the other(s) tries to guess it by suggesting letters or numbers, within a certain number of guesses. \n", "\n", "The word to guess is represented by a row of dashes, representing each letter of the word. In most variants, proper nouns, such as names, places, and brands, are not allowed. Slang words, sometimes referred to as informal or shortened words, are also not allowed. \n", "\n", "If the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions. If the suggested letter does not occur in the word, the other player draws one element of a hanged man stick figure as a tally mark. \n", "\n", "The player guessing the word may, at any time, attempt to guess the whole word. If the word is correct, the game is over and the guesser wins. Otherwise, the other player may choose to penalize the guesser by adding an element to the diagram. \n", "\n", "On the other hand, if the other player makes enough incorrect guesses to allow his opponent to complete the diagram, the game is also over, this time with the guesser losing. However, the guesser can also win by guessing all the letters or numbers that appears in the word, thereby completing the word, before the diagram is completed. \n", "\n", "\"lc\"\n", "\n", "The above image shows an example game of Hangman in progress. The underlined letters appear in the word in their correct places, while the crossed-out letters do not appear, and each crossed-out letter corresponds to one part of the drawing. In this case, the secret word is “hangman”." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## -----------------------------------------------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Approach to solve the game**\n", "\n", "In this game, we set a secret word. \n", "The guesser first has to input his name, and then, will be asked to guess any alphabet. \n", "If the random word contains that alphabet, it will be shown as the output (with correct placement) else the program will ask you to guess another alphabet. \n", "Guesser will be given length_of_word+2 turns (can be changed accordingly) to guess the complete word. \n", "If the guesser guesses all the letters in the word within the number of turns then he wins the game else loses the game." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## -----------------------------------------------------------------------------------------------------------------------------" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What is your name? Babloo\n", "Hello Babloo, Time to play hangman!\n", "Start guessing...\n", "HINT: word is the name of a fruit\n", "\n", "_\n", "_\n", "_\n", "_\n", "_\n", "Guess a character: z\n", "Wrong guess\n", "You have 6 more guesses\n", "_\n", "_\n", "_\n", "_\n", "_\n", "Guess a character: a\n", "a\n", "_\n", "_\n", "_\n", "_\n", "Guess a character: r\n", "Wrong guess\n", "You have 5 more guesses\n", "a\n", "_\n", "_\n", "_\n", "_\n", "Guess a character: p\n", "a\n", "p\n", "p\n", "_\n", "_\n", "Guess a character: q\n", "Wrong guess\n", "You have 4 more guesses\n", "a\n", "p\n", "p\n", "_\n", "_\n", "Guess a character: p\n", "a\n", "p\n", "p\n", "_\n", "_\n", "Guess a character: c\n", "Wrong guess\n", "You have 3 more guesses\n", "a\n", "p\n", "p\n", "_\n", "_\n", "Guess a character: e\n", "a\n", "p\n", "p\n", "_\n", "e\n", "Guess a character: j\n", "Wrong guess\n", "You have 2 more guesses\n", "a\n", "p\n", "p\n", "_\n", "e\n", "Guess a character: l\n", "a\n", "p\n", "p\n", "l\n", "e\n", "You won the game :-)\n" ] } ], "source": [ "def play_hangman():\n", " name = input(\"What is your name? \")\n", " print(f\"Hello {name}, Time to play hangman!\")\n", " print(\"Start guessing...\")\n", " print(\"HINT: word is the name of a fruit\\n\")\n", " # here we set the secret word\n", " word = \"apple\"\n", " # creates an variable with an empty value\n", " guesses = \"\"\n", " # determine the number of turns\n", " turns = len(word) + 2\n", "\n", " # Create a while loop\n", " # check if the turns are more than zero\n", " while turns > 0:\n", " # make a counter that starts with zero\n", " failed = 0\n", " # for every character in secret_word\n", " for char in word:\n", " # see if the character is in the players guess\n", " if char in guesses:\n", " # print then out the character\n", " print(char)\n", " else:\n", " # if not found, print a dash\n", " print(\"_\")\n", " # and increase the failed counter with one\n", " failed += 1\n", "\n", " # if failed is equal to zero\n", " # print You Won\n", " if failed == 0:\n", " print(\"You won the game :-)\")\n", " # exit the script\n", " break\n", "\n", " # ask the user go guess a character\n", " guess = input(\"Guess a character: \")\n", " # set the players guess to guesses\n", " guesses += guess\n", " # if the guess is not found in the secret word\n", " if guess not in word:\n", " # turns counter decreases with 1\n", " turns -= 1\n", " # print wrong\n", " print(\"Wrong guess\")\n", " # how many turns are left\n", " print(f\"You have {turns} more guesses\")\n", "\n", " # if the turns are equal to zero\n", " if turns == 0:\n", " # print \"You Loose\"\n", " print(\"You lost the game :-(\")\n", "\n", "\n", "if __name__ == \"__main__\":\n", " play_hangman()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } } }, "nbformat": 4, "nbformat_minor": 1 }