November 24th, 2004, 05:12 PM
anoying thing to do
i am doing a hangman game
the thing is like this:
i have the word called word
i have the letter the user inputs
well i want to generate a string thats contains the letter in its correct and the rest i want "_", and i did that, but i want to maintain this and sum the next input and that is what i cant
since you probably didnt understand what i wrote i will make an example:
the word is "elephant"
i input letter e so the program will show me this:
e _ e _ _ _ _ _
and then i input "a" and the program should show this:
e _ e _ _ a_ _
but i cant do it, i am using too much "for" and "while" and "if" functions and i cant make it work
thanks for your help
November 24th, 2004, 05:30 PM
I thought for a bit, and I can't see a nice way of doing it as you describe - summing the two (and more) seems quite hard as you are dealing with positions of letters, and so on.
But you can do it quite neatly if you reverse it slightly - Instead of adding the letters as they are guessed correctly, hide the letters they have not yet guessed. So:
Keep a store of correctly guessed letters so far.
For each guess that is correct, add the letter to the store.
Copy the word.
Go through the letters in the word, and if the user has not yet guessed that letter, replace it with an underscore, e.g.
[Edit: Did I really join this forum in November last year? ]
word = "elephant"
letters = 
# say the user guessed "e" last time:
# Now they guess "a":
letter = "a"
# Copy the word with all the letters visible
output = word
# Hide the letters that have not yet been guessed
for each letter in output:
if letter not in letters:
output = output.replace(letter, "_")
# output = "e_e__a__"
November 24th, 2004, 06:52 PM
November 24th, 2004, 07:06 PM
It's just where lots of reading of other people's suggestions on other forums has got me
Originally Posted by ivanhope