<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: newbiegreen4ever</title>
    <description>The latest articles on DEV Community by newbiegreen4ever (@newbiegreen4ever).</description>
    <link>https://dev.to/newbiegreen4ever</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F663418%2F037ba6e1-c4d7-47f1-bac9-5a9a12ef2f30.png</url>
      <title>DEV Community: newbiegreen4ever</title>
      <link>https://dev.to/newbiegreen4ever</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/newbiegreen4ever"/>
    <language>en</language>
    <item>
      <title>Python Hangman Game</title>
      <dc:creator>newbiegreen4ever</dc:creator>
      <pubDate>Thu, 08 Jul 2021 09:24:33 +0000</pubDate>
      <link>https://dev.to/newbiegreen4ever/python-hangman-game-24bn</link>
      <guid>https://dev.to/newbiegreen4ever/python-hangman-game-24bn</guid>
      <description>&lt;p&gt;2021.07.08 16:38&lt;/p&gt;

&lt;h1&gt;
  
  
  Hangman game
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I wrote a command-line word-guessing game using basic features of Python. It is a project under the Python Bundle - Python I of &lt;a href="https://codingnomads.co/"&gt;codingnomads&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I wrote this follow-up article as my learning log.&lt;/p&gt;

&lt;h2&gt;
  
  
  1) Generate a random word
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;reference: &lt;a href="https://www.geeksforgeeks.org/pulling-a-random-word-or-string-from-a-line-in-a-text-file-in-python/"&gt;Geeksforgeeks - pulling a random word or a string from a line in a text file&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Since I have some minimal experiences with JavaScript before, I believe that I have the ability to walk the extra mile and to do the optional part.&lt;/p&gt;

&lt;p&gt;I wrote a list of words separated by space and store them in a .txt file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hangman.txt
programming strong codingnomads rewarding frustrating
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The list of words in the .txt file is treated like a list. Random integer is used to randomly pick one word out of the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import random
with open(r"C:\Users\User\Desktop\codingnomads\Python-101\projects\hangman.txt", "r") as file:
    data = file.read()
    words = data.split()
    words_pos = random.randint(0, len(words) - 1)
    print(f"position: {words_pos}")
    print(f"word at position: {words[words_pos]}")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1.1) details with reading the path
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;reference: &lt;a href="https://stackoverflow.com/questions/37400974/unicode-error-unicodeescape-codec-cant-decode-bytes-in-position-2-3-trunca"&gt;stackoverflow - unicode escape&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An error will occur if the path is not treated by unicode escape &lt;code&gt;r"path"&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2) Split a word into a list of character
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;reference: &lt;a href="https://stackoverflow.com/questions/113655/is-there-a-function-in-python-to-split-a-word-into-a-list"&gt;stackoverflow - is there a function in python to split a word into a list&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The syntax of Python is really intuitive. The syntax to split a word into a list of character is simply &lt;code&gt;list(word)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.1) why list? why not empty string?
&lt;/h3&gt;

&lt;p&gt;Because strings in Python are immutable, which means that I cannot change the content of an empty string. List is a simple structure which supports item assignment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# container = "    "
# container[1] = "a"
# TypeError: 'str' object does not support item assignment
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3) Check input
&lt;/h2&gt;

&lt;p&gt;I use a while-loop inside the main while-loop to check user input.&lt;/p&gt;

&lt;p&gt;The user can only input:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;alphabet &lt;code&gt;guess.isalpha()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;a single alphabet &lt;code&gt;len(guess) == 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;alphabet not yet guessed correctly &lt;code&gt;guess in container&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while container != word:
    guess = input("Guess an alphabet: ").lower()
    while not guess.isalpha() or len(guess) &amp;gt; 1:
        guess = input("We only allow single character input. Please guess again: ").lower()
  while guess.lower() in container:
        guess = input(f"{guess.upper()} is already guessed. Please guess another alphabet: ").lower()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.1) remember indentation level
&lt;/h3&gt;

&lt;p&gt;Indentation level is important in Python. &lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;guess in container&lt;/code&gt; loop is indented below &lt;code&gt;not guess.isalpha() or len(guess) &amp;gt; 1&lt;/code&gt; loop, Python will only check guess which is not an alphabet and with a length longer than 1. The consequences are that Python will only check the bad guesses are in container, and the answer will always be no. This makes the snippet not meaningful.&lt;/p&gt;

&lt;h2&gt;
  
  
  4) Create a counter for how many attempts a user has taken
&lt;/h2&gt;

&lt;p&gt;Position is important. Although Python does not have a strict namespace rule meaning that variable declared inside loop can be accessed outside loop, where to declare a variable is still important.&lt;/p&gt;

&lt;p&gt;Remeber to declare the counter outside the main while-loop &lt;code&gt;while container != word&lt;/code&gt;, otherwise the counter will be re-assigned the value of 0 in every loop. This means the counter not meaningful as it only shows &lt;code&gt;0&lt;/code&gt; then &lt;code&gt;1&lt;/code&gt; then &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  5) Return all index with same value
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;reference: &lt;a href="https://www.codegrepper.com/code-examples/python/how+to+return+all+index+with+same+value+in+list"&gt;codegrepper - how to return all index with same value in list&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Codegrepper suggests a one-line solution with &lt;code&gt;enumerate()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;indices = [i for i, x in enumerate(word) if x == guess]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;reference: &lt;a href="https://stackoverflow.com/questions/5419204/index-of-duplicates-items-in-a-python-list"&gt;stackoverflow - index of duplicate items in a python list&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Stackoverflow suggests a simple answer which uses the counter as the index to locate the element with the desired value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if elem in string_list:
     counter = 0
     elem_pos = []
     for i in string_list:
         if i == elem:
             elem_pos.append(counter)
         counter = counter + 1
     print(elem_pos)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6) Assign new value to container list
&lt;/h2&gt;

&lt;p&gt;Now I am working with mutable list not immutable string. Therefore, value of an element in a string can be reassigned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for index in indices:
        if word[index] == guess:
                container[index] = guess
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7) Display container
&lt;/h2&gt;

&lt;p&gt;It is handy to know that &lt;code&gt;print()&lt;/code&gt; function takes the argument of &lt;code&gt;sep&lt;/code&gt; (seperation) and &lt;code&gt;end&lt;/code&gt; (ending). I change the default ending from &lt;code&gt;"\n"&lt;/code&gt; to &lt;code&gt;""&lt;/code&gt; such that every element of the container will be displaced in the same line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for char in container:
    if char.isalpha():
        print(f" {char} ", sep=" ", end="")
    else:
        print(" _  ", sep=" ", end="")
print("\n")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  8) End of game message
&lt;/h2&gt;

&lt;p&gt;Python can easily turn a string into a list by &lt;code&gt;list(str)&lt;/code&gt;, and turn a list into a string by &lt;code&gt;"".join(list)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I want a string here because I want to apply the &lt;code&gt;str.format&lt;/code&gt; and &lt;code&gt;str.upper()&lt;/code&gt; methods to make my output prettier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if container == word:
    print(f"""
            Congratulation! You made it!
            The word is {"".join(word).upper()!r}!
    """)

else:
    print(f"""
            Your guesses are close.
            The word is {"".join(word).upper()!r}.
    """)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Full code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import random
with open(r"C:\Users\User\Desktop\codingnomads\Python-101\projects\hangman.txt", "r") as file:
    data = file.read()
    words = data.split()
    words_pos = random.randint(0, len(words) - 1)
    # print(f"position: {words_pos}")
    # print(f"word at position: {words[words_pos]}")

word = list(words[words_pos])
container = list(len(word) * " ")
limit = len(word) + 5
counter = 0 
# print(word)
# print(container)

print(f"""
        Welcome! Let's play Hangman, the word-guessing game.
        You will have {limit} chances to guess the word
        Let's begin! 
""")

for char in word:
    print(" _ ", sep=" ", end="")
print("\n\n\n")

while container != word:
    guess = input("Guess an alphabet: ").lower()
    while not guess.isalpha() or len(guess) &amp;gt; 1:
        guess = input("We only allow single character input. Please guess again: ").lower()
    while guess.lower() in container:
        guess = input(f"{guess.upper()} is already guessed. Please guess another alphabet: ").lower()
    counter += 1
    print(f"Number of guessess: {counter}")

        if counter &amp;gt; limit:
        print(f"You have already guessed {counter} times. You run out of time.")
        break

    for char in word:
        if guess == char:
            indices = [i for i, x in enumerate(word) if x == guess]
            # print(indices)
            for index in indices:
                if word[index] == guess:
                    container[index] = guess

    for char in container:
        if char.isalpha():
            print(f" {char} ", sep=" ", end="")
        else:
            print(" _  ", sep=" ", end="")
    print("\n")

if container == word:
    print(f"""
            Congratulation! You made it!
            The word is {"".join(word).upper()!r}!
    """)
    # how to join list into a string
    # https://www.simplilearn.com/tutorials/python-tutorial/list-to-string-in-python
else:
    print(f"""
            Your guesses are close.
            The word is {"".join(word).upper()!r}.
    """)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you for reading my article. Please feel free to leave any comment.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
