{"id":18,"date":"2022-03-07T16:10:04","date_gmt":"2022-03-07T16:10:04","guid":{"rendered":"https:\/\/pratimeshtiwari.com\/blogs\/?p=18"},"modified":"2022-07-07T16:05:44","modified_gmt":"2022-07-07T15:05:44","slug":"build-hangman-with-ease-in-python-and-save-user-record-in-a-text-file-and-mysql-database","status":"publish","type":"post","link":"https:\/\/pratimeshtiwari.com\/blogs\/?p=18","title":{"rendered":"Hangman Game in Python and store user records in a text file"},"content":{"rendered":"\n<p class=\"has-medium-font-size\">A&nbsp;<strong>Hangman Game On Python<\/strong>&nbsp;is about guessing letters (A-Z) to form the words. If the player guesses the right letter that is within the word, the letter appears at its correct position. The user has to guess the correct word until a man is hung, then the game is over.<\/p>\n\n\n\n<p> <\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Modules Used<\/strong> : <\/p>\n\n\n\n<ul class=\"has-medium-font-size wp-block-list\"><li><strong>random<\/strong> : pre-installed<\/li><\/ul>\n\n\n\n<p class=\"has-medium-font-size\">Python Requirement : 3.8 and above.<\/p>\n\n\n\n<h4 class=\"has-medium-font-size wp-block-heading\" id=\"source-code\"><strong>Source Code : <\/strong><\/h4>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Hangman.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-medium-font-size\">def Hangman():\n\n    import random\n\n    f=open('wordlist.txt','r') #just create a file with the words           \n                                to be used in the game\n    f1=open('record.txt','a')\n\n    words=f.read().splitlines() \n    #using splitlines() instead of f.readlines() to avoid end of \n     line character while viewing the written data \n    \n    word = random.choice(words[:-1])\n    comma=\",\"\n\n    allowed_errors = 3      \n    User=input(\"Enter Your Name : \")\n    guesses = []\n\n    done = False \n \n    while not done:\n        for letter in word:\n            if letter.lower() in guesses:\n                print(letter,end=\" \")\n            else:\n                print(\"_\",end=\" \")\n        print(\"\")\n\n\n        guess = input(f\"Allowed Errors Left{allowed_errors},Next\n                      Guess : \")\n        guesses.append(guess.lower())\n\n        if guess.lower() not in word.lower():\n            allowed_errors -= 1\n\n            if allowed_errors == 0:\n                break\n\n        done = True\n\n        for letter in word:\n            if letter.lower() not in guesses:\n                done = False\n\n    if done:\n        print(f\"You have found the word ! It was {word}!\")\n        f1.write(f\"{User}{comma}Guessed the word ! It was\n                 {word}!\\n\")\n    else:\n        print(f\"Game Over ! The word was {word} ! \\n\")\n        f1.write(f\"{User}{comma}failed to Guess the word! It was\n                 {word}! \\n\")<\/pre>\n\n\n\n<p class=\"has-medium-font-size\">Hangman()<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>To View User&#8217;s Record<\/strong> : <\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Record.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-medium-font-size\">def Record():\n    f=open('record.txt','r')\n    record=f.read().splitlines()\n    for i in record:\n        print(i)\nRecord()\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;Hangman Game On Python&nbsp;is about guessing letters (A-Z) to form the words. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":148,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[5,8,7],"class_list":["post-18","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-coding","tag-hangman","tag-python"],"_links":{"self":[{"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":8,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":169,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions\/169"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pratimeshtiwari.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}