{"id":1658,"date":"2023-10-07T14:53:09","date_gmt":"2023-10-07T05:53:09","guid":{"rendered":"https:\/\/www.munchou.com\/biwa-chan\/?p=1658"},"modified":"2023-10-07T14:53:11","modified_gmt":"2023-10-07T05:53:11","slug":"python-make-a-hangman-game-part-3-input-and-display-the-right-picked-letters","status":"publish","type":"post","link":"https:\/\/www.munchou.com\/biwa-chan\/?p=1658","title":{"rendered":"Python &#8211; Make a Hangman game part 3: input and display the right picked letters"},"content":{"rendered":"<p>I enjoy creating inputs. I discovered that creating inputs with while loops in them allowed to create menus that would not need to run inside a while loop. And it makes everything easier. That may sound weird, but my first &#8220;big&#8221; program with a menu had it in a while loop, so basically it would reload a new menu in the loop, hence costing memory and whatnot. Which was a bad way. I remember it even triggered an error or something fishy at some point. Bear in mind that I&#8217;m a junior, and there might be better ways to do that, but so far it&#8217;s been working well.<\/p>\n<p>For the game, we need the user to enter ONE letter at a time. And not any letters, one that belongs to the alphabet. It can be entered in lower or uppercase, it&#8217;s not relevant as we&#8217;ll add something to take care of that conundrum for us. So what we want is the program to accept the input only when it answers the constraints, or else the user must type in again.<\/p>\n<hr \/>\n<p style=\"padding-left: 40px;\"><strong><u>INPUT? YES! PUT IT IN!<\/u><\/strong><\/p>\n<p>Or that&#8217;s what she said. Yes, apart from being a genius, I also make high-level jokes.<br \/>So before we get to the views, in which our inputs will be placed, let&#8217;s stay in our first file and create that input and its content there. First, create a variable <span style=\"color: #003366;\"><strong>alphabet = &#8220;ABCDEFGHIJKLMNOPQRSTUVWXYZ&#8221;<\/strong><\/span><br \/>That variable will be the base of the allowed characters (which is the alphabet). We could have used <span style=\"color: #003366;\"><strong>.isalpha()<\/strong><\/span> (that checks if the character belongs to the alphabet a-z A-Z), but for the game I prefer that way in case one would want to use different characters or limit them or whatever.<br \/>Then we start a while loop. Why? Because we get out of the loop ONLY if the constraints we set are met. Else, it keeps running that loop. Which also means you need to be careful, or else the program could get stuck in an infinite loop. I like the idea, but it&#8217;s not that fun when it happens during a presentation. Which never happened to me. But still, the idea.<br \/>In that while loop, we set our input and the conditions to have it right\/wrong.<br \/>So this is what I&#8217;ve got for you (add it after your current code):<\/p>\n\n\n\n<div class=\"codecolorer-container python railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/><\/div><\/td><td><div class=\"python codecolorer\">alphabet <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;<\/span><br \/>\n<span class=\"kw1\">while<\/span> <span class=\"kw2\">True<\/span>:<br \/>\n&nbsp; &nbsp; letter_input <span class=\"sy0\">=<\/span> <span class=\"kw2\">input<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Enter a letter: &quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">upper<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> letter_input <span class=\"kw1\">in<\/span> alphabet <span class=\"kw1\">and<\/span> <span class=\"kw2\">len<\/span><span class=\"br0\">&#40;<\/span>letter_input<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">==<\/span> <span class=\"nu0\">1<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">break<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">elif<\/span> letter_input <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;EXIT&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; exit<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">else<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">continue<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n\n\n<p>Test it. You should get something like:<\/p>\n<p style=\"background-color: #2b2b2b;\"><span style=\"color: #ffffff;\">_ _ _ _ _ _ _ <\/span><br \/><span style=\"color: #ffffff;\">Enter a letter:<\/span><\/p>\n<p>The <strong><span style=\"color: #003366;\">.upper()<\/span><\/strong> suffix converts the input into a capital letter. Because &#8220;a&#8221; does not belong to our string <strong><span style=\"color: #003366;\">alphabet<\/span><\/strong>, it is converted into &#8220;A&#8221; and does not require anything else.<br \/>Adding <strong><span style=\"color: #003366;\">len(letter_input) == 1<\/span><\/strong> prevents the program from accepting, for example &#8220;DE&#8221;, which is part of <strong><span style=\"color: #003366;\">alphabet<\/span><\/strong> (so it would work) but is not what we want. We want ONE letter, so we limit the number of accepted characters to 1. If those constraints are met, it gets out of the loop.<br \/>If the user enters &#8220;exit&#8221; (regardless of which character(s) is\/are in capital), the program stops.<br \/>If none of the above, the loop starts over, so the user must enter something again. So simple, but so powerful. That&#8217;s what she&#8230; no, I won&#8217;t write it.<\/p>\n<p>What to do with that letter? We must check if it is in the chosen word or not, and store it. So I made 2 lists in which the letters will be stored, depending on whether they are right or wrong: <span style=\"color: #003366;\"><strong>wrong_letters<\/strong> <\/span>and <strong><span style=\"color: #003366;\">right_letters<\/span><\/strong>. Talk about some mysterious variables&#8230; To add a letter to a list, we do <strong><span style=\"color: #003366;\">list.append(letter)<\/span><\/strong> (where <strong><span style=\"color: #003366;\">letter<\/span> <\/strong>is our validated input).<br \/>We&#8217;ll also need to move our variable <span style=\"color: #003366;\"><strong>result<\/strong> <\/span>that will display the new word after validation.<br \/>The next step is to check if the <strong><span style=\"color: #003366;\">right_letters<\/span> <\/strong>list contains any characters, and go over <strong><span style=\"color: #003366;\">chosen_word<\/span><\/strong> to check if the letter matches. If so, they are added to result, if not, &#8220;_&#8221; is added. When all letters match, it means we guessed the whole word. That step has to be placed BEFORE the input, so that the user can see the empty word before being asked to enter a letter.<br \/>All of that must be put in a while loop, so that the game keeps running.<br \/>He is the updated full code:<\/p>\n\n\n\n<div class=\"codecolorer-container python railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/><\/div><\/td><td><div class=\"python codecolorer\"><span class=\"kw1\">import<\/span> json<br \/>\n<span class=\"kw1\">import<\/span> <span class=\"kw3\">random<\/span><br \/>\n<br \/>\n<br \/>\nlanguage <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;english&quot;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">def<\/span> loadWords<span class=\"br0\">&#40;<\/span>language<span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">with<\/span> <span class=\"kw2\">open<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;words.json&quot;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"kw1\">as<\/span> <span class=\"kw2\">file<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">return<\/span> json.<span class=\"me1\">load<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw2\">file<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#91;<\/span>language<span class=\"br0\">&#93;<\/span><br \/>\n<br \/>\nwords <span class=\"sy0\">=<\/span> loadWords<span class=\"br0\">&#40;<\/span>language<span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\nchosen_word <span class=\"sy0\">=<\/span> words<span class=\"br0\">&#91;<\/span><span class=\"kw3\">random<\/span>.<span class=\"me1\">randrange<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw2\">len<\/span><span class=\"br0\">&#40;<\/span>words<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n<br \/>\nalphabet <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;<\/span><br \/>\nwrong_letters <span class=\"sy0\">=<\/span> <span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span><br \/>\nright_letters <span class=\"sy0\">=<\/span> <span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">while<\/span> <span class=\"kw2\">True<\/span>:<br \/>\n&nbsp; &nbsp; result <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;&quot;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">for<\/span> c <span class=\"kw1\">in<\/span> chosen_word:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> c <span class=\"kw1\">in<\/span> right_letters:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result +<span class=\"sy0\">=<\/span> f<span class=\"st0\">&quot;{c} &quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">else<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result +<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;_ &quot;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span>f<span class=\"st0\">&quot;<span class=\"es0\">\\t<\/span>{result}&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">while<\/span> <span class=\"kw2\">True<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; letter_input <span class=\"sy0\">=<\/span> <span class=\"kw2\">input<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Enter a letter: &quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">upper<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> letter_input <span class=\"kw1\">in<\/span> alphabet <span class=\"kw1\">and<\/span> <span class=\"kw2\">len<\/span><span class=\"br0\">&#40;<\/span>letter_input<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">==<\/span> <span class=\"nu0\">1<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">break<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">elif<\/span> letter_input <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;EXIT&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">else<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">continue<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> letter_input <span class=\"kw1\">in<\/span> chosen_word <span class=\"kw1\">and<\/span> letter_input <span class=\"kw1\">not<\/span> <span class=\"kw1\">in<\/span> right_letters:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; right_letters.<span class=\"me1\">append<\/span><span class=\"br0\">&#40;<\/span>letter_input<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">elif<\/span> letter_input <span class=\"kw1\">not<\/span> <span class=\"kw1\">in<\/span> chosen_word <span class=\"kw1\">and<\/span> letter_input <span class=\"kw1\">not<\/span> <span class=\"kw1\">in<\/span> wrong_letters:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; wrong_letters.<span class=\"me1\">append<\/span><span class=\"br0\">&#40;<\/span>letter_input<span class=\"br0\">&#41;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n\n\n<p>And testing it:<\/p>\n<p style=\"background-color: #2b2b2b;\"><span style=\"color: #ffffff;\">Enter a letter: e<\/span><br \/><span style=\"color: #ffffff;\">_ _ A _ _ <\/span><br \/><span style=\"color: #ffffff;\">Enter a letter: r<\/span><br \/><span style=\"color: #ffffff;\">_ _ A _ R <\/span><br \/><span style=\"color: #ffffff;\">Enter a letter: h<\/span><br \/><span style=\"color: #ffffff;\">_ H A _ R <\/span><br \/><span style=\"color: #ffffff;\">Enter a letter: c<\/span><br \/><span style=\"color: #ffffff;\">C H A _ R <\/span><br \/><span style=\"color: #ffffff;\">Enter a letter: i<\/span><br \/><span style=\"color: #ffffff;\">C H A I R<\/span><\/p>\n<p>Yummyyyyy!<\/p>\n<p>We are getting a nice base. In the next episode, we&#8217;ll get into the <strong>VIEWS<\/strong>, add the health &#8220;bar&#8221; and render the hanged dude.<\/p>","protected":false},"excerpt":{"rendered":"<p>I enjoy creating inputs. I discovered that creating inputs with while loops in them allowed to create menus that would not need to run inside a while loop. And it makes everything easier. That may sound weird, but my first &#8220;big&#8221; program with a menu had it in a while loop, so basically it would [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1607,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[63,62,64],"_links":{"self":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts\/1658"}],"collection":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1658"}],"version-history":[{"count":15,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts\/1658\/revisions"}],"predecessor-version":[{"id":1673,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts\/1658\/revisions\/1673"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/media\/1607"}],"wp:attachment":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}