write a Hangman game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    write a Hangman game

    I've seen several versions of Hangman game written in many different computer languages like Pascal, C++, Java and even in my TI calculator ( I don't know what the code mean, but what kind of computer languages developers use). I'm really interesting in written this game using Python, however, I don't know how to start. Can someone help me write the simple version of this game with just a few secret words (without the picture of the man is being hanged ^_^)?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Hangman Search on the Python Forum. If you hurry, you'll see the list before the search expires.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      Hangman Search on the Python Forum. If you hurry, you'll see the list before the search expires.
      Well, that search expired. Here is one of the longest threads on the subject.

      Comment

      • elcron
        New Member
        • Sep 2007
        • 43

        #4
        This book has a chapter on it as well as many other chapters on how to program python with games used as examples.

        Comment

        • python101
          New Member
          • Sep 2007
          • 90

          #5
          Originally posted by bartonc
          Hangman Search on the Python Forum. If you hurry, you'll see the list before the search expires.
          The codes people used in those threats were so complex. So, I need to break the whole thing into small pieces and work on each one.

          My first question is how to reveal the * which representes for a hidden letter.
          [code=python]
          #when the program it running
          >> play('python')

          * * * * * *
          the hidden word has 6 letters
          turn: 5

          Please enter a letter: p

          #the program should print out

          You're right
          p * * * * * # I don't know how to write this
          turn: 5

          Please enter a letter: s

          #the program should print out
          You're wrong
          p * * * * * # I don't know how to write this
          turn: 4

          [/code]

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by python101
            The codes people used in those threats were so complex. So, I need to break the whole thing into small pieces and work on each one.

            My first question is how to reveal the * which representes for a hidden letter.
            [code=python]
            #when the program it running
            >> play('python')

            * * * * * *
            the hidden word has 6 letters
            turn: 5

            Please enter a letter: p

            #the program should print out

            You're right
            p * * * * * # I don't know how to write this
            turn: 5

            Please enter a letter: s

            #the program should print out
            You're wrong
            p * * * * * # I don't know how to write this
            turn: 4

            [/code]
            Ok you need to check if the letter is in the word and then if it is replace the star in the position of the letter with the actual letter:
            [code=python]
            def indexList(s, item, start = 0):
            return [i+start for (i, let) in enumerate(s[start:]) if let == item]

            base = "******"
            word = "python"
            letter = raw_input("Ente r a letter: ")
            if letter in word:
            lbase = list(base)
            for pos in indexList(word, letter):
            lbase[pos] = letter

            base = ''.join(lbase)
            [/code]

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by ilikepython
              Ok you need to check if the letter is in the word and then if it is replace the star in the position of the letter with the actual letter:
              [code=python]
              def indexList(s, item, start = 0):
              return [i+start for (i, let) in enumerate(s[start:]) if let == item]

              base = "******"
              word = "python"
              letter = raw_input("Ente r a letter: ")
              if letter in word:
              lbase = list(base)
              for pos in indexList(word, letter):
              lbase[pos] = letter

              base = ''.join(lbase)
              [/code]
              A list is one way. Here's another:[CODE=python]
              >>> base = "******"
              >>> word = "python"
              >>> guess = 'h'
              >>> idx = word.find(guess )
              >>> if idx > -1:
              ... base = base[:idx] + guess + base[idx + 1:]
              ...
              >>> base
              '***h**'
              >>> [/CODE]But that won't handle more than one occurrence of the letter in the word.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Here is a similar way that will handle multiple occurrences of a letter:[code=Python]def hintStrSub(hint _str, letter, pos_list):
                for i in pos_list:
                lst = [hint_str[:i], ]
                if len(hint_str) >= i+1:
                lst.append(hint _str[(i+1):])
                hint_str = letter.join(lst )
                return hint_str

                def indexList(s, item, i=0):
                i_list = []
                while True:
                try:
                i = s.index(item, i)
                i_list.append(i )
                i += 1
                except:
                break
                return i_list[/code]
                [code=Python]>>> idx_list = indexList('sequ entially', 'l')
                >>> idx_list
                [9, 10]
                >>> hint = '*' * len('sequential ly')
                >>> hintStrSub(hint , 'l', idx_list)
                '*********ll*'
                >>> [/code]

                Comment

                • python101
                  New Member
                  • Sep 2007
                  • 90

                  #9
                  string related question

                  [code=python]
                  a = 'hello'
                  for i in range(len(a)):
                  print a.index('l',a.i ndex('l')+i)

                  2
                  3

                  Traceback (most recent call last):
                  File "C:/Python25/test.py", line 4, in <module>
                  print a.index('l',a.i ndex('l'))
                  ValueError: substring not found
                  [/code]

                  I would like to find all indexes of letter l in the list, I got them but had this error. How can I fix this.

                  Comment

                  • ilikepython
                    Recognized Expert Contributor
                    • Feb 2007
                    • 844

                    #10
                    Originally posted by python101
                    [code=python]
                    a = 'hello'
                    for i in range(len(a)):
                    print a.index('l',a.i ndex('l')+i)

                    2
                    3

                    Traceback (most recent call last):
                    File "C:/Python25/test.py", line 4, in <module>
                    print a.index('l',a.i ndex('l'))
                    ValueError: substring not found
                    [/code]

                    I would like to find all indexes of letter l in the list, I got them but had this error. How can I fix this.
                    This is a neat little function that does what I think you want:
                    [code=python]
                    def indexList(s, item, i=0):
                    i_list = []
                    while True:
                    try:
                    i = s.index(item, i)
                    i_list.append(i )
                    i += 1
                    except:
                    break
                    return i_list
                    [/code]
                    P.S. Did you take a look at the responses in your other thread? I think they address this same issue.

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by ilikepython
                      P.S. Did you take a look at the responses in your other thread? I think they address this same issue.
                      I've merge the two. Seems like the same issue to me.

                      Comment

                      • python101
                        New Member
                        • Sep 2007
                        • 90

                        #12
                        tried but it only gave me a first index occurrence of an item

                        Comment

                        • python101
                          New Member
                          • Sep 2007
                          • 90

                          #13
                          Originally posted by ilikepython
                          This is a neat little function that does what I think you want:
                          [code=python]
                          def indexList(s, item, i=0):
                          i_list = []
                          while True:
                          try:
                          i = s.index(item, i)
                          i_list.append(i )
                          i += 1
                          except:
                          break
                          return i_list
                          [/code]
                          P.S. Did you take a look at the responses in your other thread? I think they address this same issue.
                          I tried this way:

                          [code=python]
                          >>name = 'John John'
                          >>letter = 'J'
                          >>iList = indexList(name, letter)
                          >>print iList
                          [0]

                          #but it should contain
                          [0,5]
                          [/code]

                          Comment

                          • python101
                            New Member
                            • Sep 2007
                            • 90

                            #14
                            thank you very much. I've purchased a python book, it explained very well how to make this game work

                            [code=python]
                            so_far = "-" * len(word)

                            # create a new so_far to include guess
                            new = ""
                            for i in range(len(word) ):
                            if guess == word[i]:
                            new += guess
                            else:
                            new += so_far[i]
                            so_far = new
                            [/code]

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #15
                              Originally posted by python101
                              I tried this way:

                              [code=python]
                              >>name = 'John John'
                              >>letter = 'J'
                              >>iList = indexList(name, letter)
                              >>print iList
                              [0]

                              #but it should contain
                              [0,5]
                              [/code]
                              It works for me:[code=Python]>>> indexList("John John", "J")
                              [0, 5]
                              >>> [/code]

                              Comment

                              Working...