Why isn't the ASCII art behaving properly in this hangman game I invented?
#1
So I ought to swear this hangman game is written with zero errors. Why isn't the ASCII art working?

Code:
#Step 4

import random

stages = ['''
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']

end_of_game = False
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
word_length = len(chosen_word)

#TODO-1: - Create a variable called 'lives' to keep track of the number of lives left.
#Set 'lives' to equal 6.

#Testing code
print(f'Pssst, the solution is {chosen_word}.')

#Create blanks
display = []
for _ in range(word_length):
    display += "_"
a = 0
b = 6

while a <= word_length and b > 0:
    guess = input("Guess a letter: ").lower()

    #Check guessed letter
    for position in range(word_length):
        letter = chosen_word[position]
        # print(f"Current position: {position}\n Current letter: {letter}\n Guessed letter: {guess}")
        if letter == guess:
            display[position] = letter
    if letter not in chosen_word:
        b -= 1
    else:
        a += 1


    #TODO-2: - If guess is not a letter in the chosen_word,
    #Then reduce 'lives' by 1.
    #If lives goes down to 0 then the game should stop and it should print "You lose."

    #Join all the elements in the list and turn it into a String.
    print(f"{' '.join(display)}")

    #Check if user has got all letters.
    if "_" not in display:
        print("You win.")
        break
    if b == 0:
      print("You lose.")

    #TODO-3: - print the ASCII art from 'stages' that corresponds to the current number of 'lives' the user has remaining.
    print(stages[b])

This last line:

Code:
print(stages[b])

This final line simply does not work. No matter what value "b" is, the same ASCII is displayed.

Could someone help me with this?
Reply
#2
Looks like stages is kind of a list/array in python right? And I'm guessing the variable b contains a number which you'll use in the final print(stages[b]) code to get the corresponding ascii hangman by refering to the location/index using b?

I'm a bit tired to try trouble shoot it myself Smile But if you want to find out why it's always printing the same stage. I would recommend by start outputing/printing the variable b. Does it always output the same number? Then I would guess you have some issue in your code when assigning values to the variable b. Between lines 77 and 92 where you assign values to a & b.

Just a small hint I guess from reading your code.
Reply
#3
(01-11-2022, 06:38 PM)Insider Wrote: Looks like stages is kind of a list/array in python right? And I'm guessing the variable b contains a number which you'll use in the final print(stages[b]) code to get the corresponding ascii hangman by refering to the location/index using b?

I'm a bit tired to try trouble shoot it myself Smile But if you want to find out why it's always printing the same stage. I would recommend by start outputing/printing the variable b. Does it always output the same number? Then I would guess you have some issue in your code when assigning values to the variable b. Between lines 77 and 92 where you assign values to a & b.

Just a small hint I guess from reading your code.

I fixed it. Will post solution tonight.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
   why won't it let me get the largest value from a dictionary? QMark 5 6,043 02-22-2022, 11:00 PM
Last Post: moodyjudy
  [AutoIt]ASCII to Morse Code b1nary 4 17,555 01-29-2016, 06:45 PM
Last Post: b1nary