technology logo
technology logo

Hangman Game

share link

by test2022sept5th3 dot icon Updated: Nov 25, 2022

Solution Kit Solution Kit  

Kit Solution Source

python-telegram-botby python-telegram-bot

Python doticonstar image 22046 doticonVersion:v20.3doticon
License: Strong Copyleft (GPL-3.0)

We have made you a wrapper you can't refuse

Support
    Quality
      Security
        License
          Reuse

            python-telegram-botby python-telegram-bot

            Python doticon star image 22046 doticonVersion:v20.3doticon License: Strong Copyleft (GPL-3.0)

            We have made you a wrapper you can't refuse
            Support
              Quality
                Security
                  License
                    Reuse
                      secretword=""
                      currentword=""
                      cw=""
                      hangcount=0
                      letters=[]  # declare letters list
                      
                      def hanging():
                          if hangcount==1:
                              print ("----------")
                              print ("|        |")
                              print ("|       ---")
                              print ("|      |   |")
                              print ("|       ---")
                          elif hangcount==2:
                              print ("----------")
                              print ("|        |")
                              print ("|       ---")
                              print ("|      |   |")
                              print ("|       ---")
                              print ("|        |")
                              print ("|        |")
                          elif hangcount==3:
                              print ("----------")
                              print ("|        |")
                              print ("|       ---")
                              print ("|      |   |")
                              print ("|       ---")
                              print ("|        |")
                              print ("|        |")
                              print ("|       /")
                          elif hangcount==4:
                              print ("----------")
                              print ("|        |")
                              print ("|       ---")
                              print ("|      |   |")
                              print ("|       ---")
                              print ("|        |")
                              print ("|        |")
                              print ("|       / \\")  # double backslashes \\ for left leg
                          elif hangcount==5:
                              print ("----------")
                              print ("|        |")
                              print ("|       ---")
                              print ("|      |   |")
                              print ("|       ---")
                              print ("|       /|")
                              print ("|        |")
                              print ("|       / \\")  # double backslashes 
                          elif hangcount==6:
                              print ("----------")
                              print ("|        |")
                              print ("|       ---")
                              print ("|      |   |")
                              print ("|       ---")
                              print ("|       /|\\")  # double backslashes for left arm
                              print ("|        |")
                              print ("|       / \\")  # and left leg
                      
                      def clearscreen():
                          for i in range(50):
                              print('\n')
                      
                      def displayCurrentword():
                          global currentword
                          cw=""
                          for ch in currentword:
                              cw=cw+ch+" "
                          print (cw)
                      
                      def initCurrentword():
                          global currentword
                          currentword=""
                          for ch in secretword:
                              currentword=currentword+"_"
                      
                      def find_replace(letter):
                          global currentword
                          found=False
                          cw=""
                          for i in range(0,len(secretword)):
                              if secretword[i]==letter:
                                  found=True
                                  cw=cw+letter
                              else:
                                  cw=cw+currentword[i]
                          currentword=cw
                          if found==False:
                              global hangcount
                              hangcount+=1
                      
                      clearscreen()
                      print ("Welcome to Hangman!")
                      print
                      secretword=raw_input("Enter word:")
                      secretword=secretword.lower()
                      
                      initCurrentword()
                      
                      while (currentword!=secretword and hangcount<6):
                          global letter                # use global letters list
                          clearscreen()
                          if len(letters) != 0:
                              # display guessed letters (set() to remove duplicates)
                              print("Guessed letters:", ', '.join(set(letters)))
                              if hangcount == 0:
                                  print ("\n" * 4)     # empty space for hangman (for consitent layout)
                          hanging()
                          displayCurrentword()
                          letter=raw_input("Enter a letter:")
                          letter=letter.lower()
                          letters.append(letter)       # add current letter to letters list
                          find_replace(letter)
                          displayCurrentword() 
                      
                      if currentword==secretword:
                          print ("Congrats you win!")
                      else:
                          clearscreen()
                          print("Guessed letters:", ', '.join(set(letters)))  # display guessed letters
                          print ("----------")
                          print ("|        |")
                          print ("|       ---")
                          print ("|      |   |")
                          print ("|       ---")
                          print ("|       /|\\")  # double backslashes 
                          print ("|        |")
                          print ("|       / \\")  # double backslashes 
                          print ("You killed him!")
                          print ("The word was %s"%(secretword))
                      

                      Hangmanby techwithtim

                      Python doticonstar image 227 doticonVersion:Currentdoticon
                      no licences License: No License (null)

                      A simple hangman game made with python and pygame.

                      Support
                        Quality
                          Security
                            License
                              Reuse

                                Hangmanby techwithtim

                                Python doticon star image 227 doticonVersion:Currentdoticonno licences License: No License

                                A simple hangman game made with python and pygame.
                                Support
                                  Quality
                                    Security
                                      License
                                        Reuse

                                          See similar Kits and Libraries