Skip to content
← Back to Community
Hangman (not printing guessed letters)-python
Profile icon
Fire-Wizard

I have been trying to make a hangman game, Everything works fine except that it does not give the previous guessed letters in the correct places like the suppose the answer was "spoon" & you guessed the s first time you do it it will print in the proper place then you guess p the next one will look like

![image]
image

how do I bring it from that to:

image

Voters
Profile icon
Fire-Wizard
Comments
hotnewtop
Profile icon
Axrevyn

Using variables and lists

Profile icon
Axrevyn

Instead of this:

# The word here is "word" print("_ _ _ _")

You could use this:

letters = ["_","_","_","_"] print(letters[0], letters[1], letters[2], letters[3])

The output would be

____

And if a letter would be guessed, simply redefine the letter.

# Someone guessed the letter "o" letters[1] = "o" print(letters[0], letters[1], letters[2], letters[3])

Output would now be

_o__

This is just a rough mock-up, so you'll have to change it a bit to fit it into your program. I can help you with it if you'd like.

Profile icon
Fire-Wizard

@Axrevyn
I get the idea, but won't that work only with only one type of length for example:
letters = ["","","",""]
print(letters[0], letters[1], letters[2], letters[3])
↑ the above code type will only work with four letter words not with words with other lengths
I will try it to make it more flexible but if possible
give me a more flexible idea

Profile icon
Fire-Wizard

and thank you for replying

Profile icon
Axrevyn

@Aayan11
It was a rough mock-up. Would you like me to help?

Profile icon
Axrevyn

@Aayan11

import sys # Reads the number of letters lm = int(input("Enter number of letters: ")) lm2 = 0 # Defines the list letters = [None, None, None, None, None, None, None, None, None, None] #As many "None" values as you need, this one will only #support as many as 10 letters because there is only 10 None values while (lm == lm2) is False: if bool(letters[int(lm2)]) is False: letters[int(lm2)] = "_" lm2 = lm2 + 1 # This function prints the parts of the list that are not None values def word_print(): lm_check = 0 while (lm_check == lm) is False: sys.stdout.write(letters[int(lm_check)]) sys.stdout.flush() lm_check = lm_check + 1 # Call the function word_print()

In this case, just define lm as the number of letters in the word and you can let the rest of the code do its thing.

Profile icon
ParkerTaylor1

hi

Profile icon
Fire-Wizard