Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 230105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:49:17+00:00 2026-05-11T19:49:17+00:00

Just getting into python, and so I decided to make a hangman game. Works

  • 0

Just getting into python, and so I decided to make a hangman game. Works good, but I was wondering if there was any kind of optimizations I could make or ways to clean up the code. Also, if anyone could recommend a project that I could do next that’d be cool.

import sys
import codecs
import random

def printInterface(lst, attempts):
    """ Prints user interface which includes:
            - hangman drawing
            - word updater """

    for update in lst:
        print (update, end = '')

    if attempts == 1:
        print ("\n\n\n\n\n\n\n\n\n\n\n\t\t    _____________")
    elif attempts == 2:
        print ("""          
                          |
                          | 
                          |
                          |
                          |
                          |
                          |
                          |
                          |
                    ______|______""")
    elif attempts == 3:
        print ("""
            ______          
                  |
                  | 
                  |
                  |
                  |
                  |
                  |
                  |
                  |
            ______|______""")
    elif attempts == 4:
        print ("""
            ______
           |      |
           |      | 
         (x_X)    |
                  |
                  |
                  |
                  |
                  |
                  |
            ______|______""")
    elif attempts == 5:
        print ("""
            ______
           |      |
           |      | 
         (x_X)    |
           |      |
           |      |
           |      |
                  |
                  |
                  |
            ______|______""")
    elif attempts == 6:
        print ("""
            ______
           |      |
           |      | 
         (x_X)    |
           |      |
          /|      |
           |      |
                  |
                  |
                  |
            ______|______""")
    elif attempts == 7:
        print ("""
            ______
           |      |
           |      | 
         (x_X)    |
           |      |
          /|\     |
           |      |
                  |
                  |
                  |
            ______|______""")
    elif attempts == 8:
        print ("""
            ______
           |      |
           |      | 
         (x_X)    |
           |      |
          /|\     |
           |      |
          /       |
                  |
                  |
            ______|______""")
    elif attempts == 9:
        print ("""
            ______
           |      |
           |      | 
         (x_X)    |
           |      |
          /|\     |
           |      |
          / \     |
                  |
                  |
            ______|______""")

def main():
    try:
        wordlist = codecs.open("words.txt", "r")
    except Exception as ex:
        print (ex)
        print ("\n**Could not open file!**\n")
        sys.exit(0)

    rand = random.randint(1,5)
    i = 0

    for word in wordlist:
        i+=1
        if i == rand:
            break
    word = word.strip()
    wordlist.close()

    lst = []
    for h in word:
        lst.append('_ ')

    attempts = 0    
    printInterface(lst,attempts) 

    while True:
        guess = input("Guess a letter: ").strip()

        i = 0
        for letters in lst:
            if guess not in word:
                print ("No '{0}' in the word, try again!".format(guess))
                attempts += 1
                break
            if guess in word[i] and lst[i] == "_ ":
                lst[i] = (guess + ' ')
            i+=1

        printInterface(lst,attempts)

        x = lst.count('_ ')
        if x == 0:
            print ("You win!")
            break
        elif attempts == 9:
            print ("You suck! You iz ded!")
            break

if __name__ == '__main__':
    while True:
        main()
        again = input("Would you like to play again? (y/n):  ").strip()
        if again.lower() == "n":
            sys.exit(1)
        print ('\n')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:49:18+00:00Added an answer on May 11, 2026 at 7:49 pm

    First idea: ASCII art

    The things special to Python are regular expression syntax and range() function, as well as [xxx for yyy in zzz] array filler.

        import re
    
        def ascii_art(attempt):
            return re.sub(r'\d', '', re.sub('[0{0}].' \
                .format(''.join([str(e) for e in range(attempt + 1, 10)])), ' ', """
                    3_3_3_3_3_3_
                   4|      2|
                   4|      2| 
                 4(4x4_4X4)    2|
                   5|      2|
                  6/5|7\     2|
                   5|      2|
                  8/ 9\     2|
                          2|
                          2|
                    1_1_1_1_1_1_1|1_1_1_1_1_1_
        """))
    
        for i in range(1, 10): 
            print(ascii_art(i)) 
    

    Second idea: loops

    Use enumerate for word reading loop. Use

    for attempt in range(1, 10):
        # inside main loop
        ...
    print ('you suck!')
    

    as the main loop. Operator break should be used with care and not as replacement for for!

    Unless I miss something, the structure of

        for letters in lst:
            if guess not in word:
                ...
                break
            if guess in word[i]:
                ...
    

    will be more transparent as

        if guess not in word:
                 ...
        else:
             index = word.find (guess)
             ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It turns out that this is best done by reversing… May 11, 2026 at 11:56 pm
  • Editorial Team
    Editorial Team added an answer When you throw an object, you're actually throwing a copy… May 11, 2026 at 11:56 pm
  • Editorial Team
    Editorial Team added an answer Here's a simple example of how to use the BackgroundWorker… May 11, 2026 at 11:56 pm

Related Questions

I started off programming in Basic on the ZX81 , then BASICA , GW-BASIC
I'm trying to use python ctypes to use these two C functions from a
I've been trying to learn Python, and while I'm enthusiastic about using closures in
I'm trying to get Google AppEngine to work on my Debian box and am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.