hangman.py

Created by emrio

Created on February 12, 2020

879 Bytes

Play the Hangman game!


from math import *
def play_hangman():
  p=print
  clr="\n"*50
  ttl="         ** Hangman **\n\n\n"
  nttl=clr+ttl
  inpt=lambda q:input("Press 'HOME' to exit\n\n"+q)
  p(nttl+"\n"*2)
  w=inpt("Enter a word: ")
  if len(w)==0:return hm()
  fnd=[False]*len(w)
  err=[]
  p(clr)
  while True:
    t=nttl+"Word: "
    for i in range(len(w)):
      t+=w[i]if fnd[i]else"_"
    t+="\n"*3+"Errors:\n"+", ".join(err)+"\n"
    p(t)
    if all(fnd):
      p("You won!\n")
      break
    elif len(err)>=10:
      p("Lost, it was '"+w+"'\n")
      break
    i=inpt("Input: ")
    if len(i)!=1:pass
    elif i in w:
      for j in range(len(w)):
        if w[j]==i:fnd[j]=True
    elif i not in err:
      err.append(i)
  y=input("Play again? (Press 'y')")
  if y=="y":return play_hangman()
  p("Bye!")

def hmpg():
  try:
    play_hangman()
  except:
    print("Bye")
hmpg()

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.