#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Derby, UK
    Posts
    3
    Rep Power
    0

    ASCII Art not displaying correctly in 'Hangman' game


    Hi everyone!

    I'm a beginner to Python and it's going quite well so far. One of my tasks is to create a 'Hangman' game to demonstrate the use of lists amongst other things.

    I used a tuple of 8 elements, each being a bit of ASCII art of the gallows at different stages of the man being hanged.

    The problem is they are not displaying correctly when the program is run! I can't for the life of me understand why and it's frustrating me, so any suggestions would be great. Thanks!

    Here is my code:

    Code:
    #imports
    import random
    
    #constants
    HANGMAN = (
    """
    ------
    |    |
    |
    |
    |
    |
    |
    |
    |
    ----------
    """,
    """
    ------
    |    |
    |    0
    |
    |
    |
    |
    |
    |
    ----------
    """,
    """
    ------
    |    |
    |    0
    |   -+-
    |
    |
    |
    |
    |
    ----------
    """,
    """
    ------
    |    |
    |    0
    |   -+-
    |  /
    |
    |
    |
    |
    ----------
    """,
    """
    ------
    |    |
    |    0
    |   -+-
    |  /   \
    |
    |
    |
    |
    ----------
    """,
    """
    ------
    |    |
    |    0
    |   -+-
    |  / | \
    |    |
    |
    |
    |
    ----------
    """,
    """
    ------
    |    |
    |    0
    |   -+-
    |  / | \
    |    |
    |   /
    |  |
    |
    ----------
    """,
    """
    ------
    |    |
    |    0
    |   -+-
    |  / | \
    |    |
    |   / \
    |  |   |
    |
    ----------
    """)
    
    MAX_WRONG = len(HANGMAN) - 1
    
    WORDS = ("PYTHON", "TRIGONOMETRY", "PHYSICS", "COMPUTING", "CHOCOLATE", "CRISPS", \
             "ANTIDISESTABLISHMENTARIANISM", "PINEAPPLE", "PAPER", "CURTAIN", "BOAT", \
             "FLAGS", "AFTERSHAVE", "STOCKING", "BULB", "GOAT", "LEAF", "JAPAN", "BOMB", \
             "GLASS", "PENCIL", "SOCK", "MODEL", "PHONE", "RUBBER")
    
    #initialise variables
    word = random.choice(WORDS) #the word to be guessed
    
    so_far = "-" * len(word) #one dash per letter in word
    
    wrong = 0 #number of wrong guesses the player has made
    
    used = []
    
    #main loop
    print("Welcome to Hangman!")
    
    while wrong < MAX_WRONG and so_far != word:
        print(HANGMAN[wrong])
        print("\nLetters used so far:", used)
        print("\nSo far the word is:", so_far)
    
        #getting the players guess
        guess = input("\n\nEnter your guess: ")
        guess = guess.upper() #because the words are in uppercase
    
        while guess in used:
            print("You've already guessed the letter", guess)
            guess = input("\n\nEnter your guess: ")
            guess = guess.upper()
    
        used.append(guess)
    
        #checking the guess
        if guess in word:
            print("\nYep", guess, "is in the word!\n")
    
            #create a new so_far
            new = ""
    
            for i in range(len(word)):
                if guess == word[i]:
                    new += guess
                else:
                    new += so_far[i]
            so_far = new
    
        else:
            print("\nNope,", guess, "isn't in the word!")
            wrong += 1
    
    if wrong == MAX_WRONG:
        print(HANGMAN[wrong])
        print("\nYou failed to prevent this poor man's suffering, shame on you!")
    else:
        print("Wahey! You got it!")
    
    print("\nThe word was", word)
    
    input("\n\nPress the enter key to exit.")
    An example:

    Code:
    ------
    |    |
    |    0
    |   -+-
    |  / | |    |
    |   / |  |   |
    |
    ----------
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480

    8<(


    First, expressions in parens are continued across lines.
    Your backslashes in the word list are not needed. Try to avoid line continuation marks!

    The backslash arms and legs are line continuation marks even though they're in triple quotes. Solution, hang on the left (row reversals computed by j):
    Code:
       |."1([;._2~LF&=)O
        ------
        |    |
             |
             |
             |
             |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
             |
             |
             |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
       -+-   |
             |
             |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
       -+-   |
          /  |
             |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
       -+-   |
      \   /  |
             |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
       -+-   |
      \ | /  |
        |    |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
       -+-   |
      \ | /  |
        |    |
         /   |
          |  |
             |
    ----------
          ,'''
              
        ------
        |    |
        0    |
       -+-   |
      \ | /  |
        |    |
       \ /   |
      |   |  |
             |
    ----------
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Derby, UK
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    First, expressions in parens are continued across lines.
    Your backslashes in the word list are not needed. Try to avoid line continuation marks!

    The backslash arms and legs are line continuation marks even though they're in triple quotes. Solution, hang on the left (row reversals computed by j):
    Code:
       |."1([;._2~LF&=)O
        ------
        |    |
             |
             |
             |
             |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
             |
             |
             |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
       -+-   |
             |
             |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
       -+-   |
          /  |
             |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
       -+-   |
      \   /  |
             |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
       -+-   |
      \ | /  |
        |    |
             |
             |
             |
    ----------
          ,'''
           '''
        ------
        |    |
        0    |
       -+-   |
      \ | /  |
        |    |
         /   |
          |  |
             |
    ----------
          ,'''
              
        ------
        |    |
        0    |
       -+-   |
      \ | /  |
        |    |
       \ /   |
      |   |  |
             |
    ----------
    Aha! I see, I'm kicking myself now! Thanks for the advice, I'll try to avoid line continuation marks where possible from now on.

    Brilliant solution, thank you.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    479
    Rep Power
    33
    You can also add the appropriate part for each wrong guess.
    Code:
    def print_hangman(list_in, wrong_guess):
        print "*"*30
        print "".join(list_in)
        lit = "%d wrong guess" % (wrong_guess)
        if 1 != wrong_guess:
            lit += "es"
        print lit
    
    hangman_pic='''
     
      +---+
      |   |
          |
          |
          |
          |
          |
    ========='''
    
    wrong_guess_pic = [("O", 21),
                       ("|", 29),
                       ("/", 28),
                       ("\\", 30),
                       ("|", 37),
                       ("/", 44),
                       ("\\", 46)]
    
    new_pic = list(hangman_pic)
    wrong_guess=0
    
    ## simulate wrong guesses
    for ctr in range(len(wrong_guess_pic)):
        tup = wrong_guess_pic[wrong_guess]
        new_pic[tup[1]] = tup[0]                         ## insert position = figure
        wrong_guess += 1
    
        print_hangman(new_pic, wrong_guess)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Derby, UK
    Posts
    3
    Rep Power
    0
    Originally Posted by dwblas
    You can also add the appropriate part for each wrong guess.
    Code:
    def print_hangman(list_in, wrong_guess):
        print "*"*30
        print "".join(list_in)
        lit = "%d wrong guess" % (wrong_guess)
        if 1 != wrong_guess:
            lit += "es"
        print lit
    
    hangman_pic='''
     
      +---+
      |   |
          |
          |
          |
          |
          |
    ========='''
    
    wrong_guess_pic = [("O", 21),
                       ("|", 29),
                       ("/", 28),
                       ("\\", 30),
                       ("|", 37),
                       ("/", 44),
                       ("\\", 46)]
    
    new_pic = list(hangman_pic)
    wrong_guess=0
    
    ## simulate wrong guesses
    for ctr in range(len(wrong_guess_pic)):
        tup = wrong_guess_pic[wrong_guess]
        new_pic[tup[1]] = tup[0]                         ## insert position = figure
        wrong_guess += 1
    
        print_hangman(new_pic, wrong_guess)
    Thanks for the suggestion, I'll try and implement that at a later date, I'm only just now learning about defining functions and some of the other code you've used.

IMN logo majestic logo threadwatch logo seochat tools logo