hangman.py

Created by squarepoint

Created on April 13, 2023

1.31 KB

A cool and polished text based hangman game


A=["\n\n\n\n\n\n- - - - -","\n  |\n  |\n  |\n  |\n  |\n- - - - -","\n- + - - -\n  |\n  |\n  |\n  |\n- - - - -","\n- + - - -\n  |     |\n  |\n  |\n  |\n- - - - -","\n- + - - -\n  |     |\n  |     O\n  |\n  |\n- - - - -","\n- + - - -\n  |     |\n  |     O\n  |     |\n  |\n- - - - -","\n- + - - -\n  |     |\n  |     O\n  |    /|\n  |    /\n- - - - -","\n- + - - -\n  |     |\n  |     O\n  |    /|\ \n  |    / \ \n- - - - -"]
def game():
  w=input("Enter word: ")
  print("\n")
  a=" "
  x=""
  for i in range(len(w)):
    if w[i]==" ":x+=" "
    else:x+="_"
  d=0
  while True:
    print(A[d])
    if d>6:
      print("\n  YOU LOST\n")
      break
    if x==w:
      print("\n  YOU WON\n")
      break
    print("Word: "+x+"\n")
    print("Letters already used:"+a+"\n")
    g=""
    g=input("Enter a letter: ")
    while g in a or len(g)>1:
      if g in a:print("You already used this letter")
      else:print("You must enter one letter")
      g=input("Enter a letter: ")
    a+=g
    if g in w:
      x2=""
      for i in range(len(w)):
        if w[i]==g:x2+=g
        else:x2+=x[i]
      x=x2
    else:
      d+=1
  print("The word was "+w+"\n")

while True:
  print("\n"*2+"    []"+"="*17+"[]\n    ||     HANGMAN     ||\n    []"+"="*17+"[]\n\n")
  game()
  e=input("Play again? (y/n) ")
  if e!="yes"and e!="y":break

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.