Hangman (not printing guessed letters)-python
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
how do I bring it from that to:
Instead of this:
# The word here is "word" print("_ _ _ _")
You could use this:
letters = ["_","_","_","_"] print(letters, letters, letters, letters)
The output would be
And if a letter would be guessed, simply redefine the letter.
# Someone guessed the letter "o" letters = "o" print(letters, letters, letters, letters)
Output would now be
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.
@Axrevyn I get the idea, but won't that work only with only one type of length for example:
letters = ["","","",""]
print(letters, letters, letters, letters)
↑ 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
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.