Reveal all instances of that letter in the series if user request in hangman game python?

Hello All
i wrote the code for hangman game in python.

how to achieve if user request the letter and have to reveal instance of letter ?

Thanks you
vishnu kalakotaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gelonidaCommented:
If I got it right I would do something like that:

in one variable I would store the random word, that you picked.
example:
to_guess = "difficult"

Open in new window


in an other variable I would store what was guessed so far, (which is initially nothing)
guessed = [ '.' ] * len(to_guess) 

Open in new window

# this is a list of as many '.' characters as you have letters in the word you have to guess.

However it would be best for learning and for helping if you post what you wrote so far.


whenever the user enters a letter you check at which position this letter exists in the word to guess and you set the character at the same position in the variable guessed from '.' to the given letter.
Walter RitzelSenior Software EngineerCommented:
Assuming you have 3 variables: one with the word to guess, one with the same size, but filled with * and the letter inputed by user:
word_to_guess = 'banana'
guess_char = '*'
user_input = 'a'
guess = ''

for i,c in enumerate(word_to_guess):
     if c == user_input:
           guess += user_input
     else:
           guess += guess_char

print guess           

Open in new window

Mark BradyPrincipal Data EngineerCommented:
Here is an easy function you can use to do what you want.

def run(self):		
		# you can set this value dynamically but for testing set it here
		word_to_guess = 'easy'

		values = []
		for i in range(len(word_to_guess)):
			values.append('_')

		guessed_all = False
		amount_guessed = 0

		while not guessed_all:			
			guess = raw_input('Enter a letter: ')
			found_one = False
			for i in range(len(word_to_guess)):
				if guess.lower() == word_to_guess[i]:
					values[i] = guess
					amount_guessed += 1
					found_one = True				
			temp = ''
			if not found_one:
				print 'guess again'

			if '_' not in values:
				guessed_all = True;
				print 'Congratulations!'
				result = ''
				for value in values:
					result += value
				print result
			else:
				print 'you have guessed ' + str(amount_guessed) + ' so far'
			
			if not guessed_all:
				for value in values:
					temp += value
				print temp

Open in new window


Put that function in a python file and call it. Have fun

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vishnu kalakotaAuthor Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.