I have been working my way through projects on Python and now I want to start on hangman. I have not started the code yet because of an issue I am having trouble finding a solution. I want a hangman game where there is a list of random words and a random word is chosen. I want the person to be able to guess a letter and then replace the "_" with that letter at the correct index. For example: The word is "apple" so "_____" but if you guess a then "a____":
I would also like to be able to place a letter in all the times it is in the word like "p" is in apple twice so if guess is "p" it would be "_pp__". I want to try and do all the rest of code myself I just would like to know how to do this small part so I can then apply it.
I did it starting the word 'apple' as a list ['a','p','p','l','e'] and every time a letter is a match I change it for '*'.
Try by yourself first and then I'll post my solution.
My solution:
def checkLetter(letter, word, guess_word):
for c in word:
if c == letter:
guess_word[word.index(c)] = c
word[word.index(c)] = '*'
return guess_word
word = list('apple')
guess_word = ['_' for x in word]
print(guess_word)
while '_' in guess_word:
guess = input('Letter: ')
print(checkLetter(guess, word, guess_word))
Thanks for the help but if you don't mind, could you please elaborate on:
while '_' in guess_word:
guess = input('Letter: ')
print(checkLetter(guess, word, guess_word))
Particularly:
while '_' in guess_word:
Thanks.
(Jun-24-2018, 01:00 AM)gontajones Wrote: [ -> ]I did it starting the word 'apple' as a list ['a','p','p','l','e'] and every time a letter is a match I change it for '*'.
Try by yourself first and then I'll post my solution.
My solution:
def checkLetter(letter, word, guess_word):
for c in word:
if c == letter:
guess_word[word.index(c)] = c
word[word.index(c)] = '*'
return guess_word
That would not work for letters that show more than once in a word. And why are you using
camelCase
? - it's un-Pythonic
def check_letter(letter, word, guess_word):
for idx, c in enumerate(word):
if c == letter:
guess_word[idx] = c
....
Quote:while '_' in guess_word:
This will keep asking for new guess(letter) until there is no more '_' in the guess_word (the word is formed).
Quote:That would not work for letters that show more than once in a word. And why are you using camelCase? - it's un-Pythonic
Did you check it? I tested with some words and it's ok.
Thanks for the camelCase tip.
(Jun-24-2018, 05:05 PM)gontajones Wrote: [ -> ]Quote:while '_' in guess_word:
This will keep asking for new guess(letter) until there is no more '_' in the guess_word (the word is formed).
OK, sorry, my mistake - I missed the part that you change the original word (I was talking about
checkLetter
function) - but calling
word.index( c )
is unneeded if you use
enumerate
- and calling it twice is wasteful. You are building a mechanism that Python provides you under the hood.
I wrote a console hangman game a while ago as part of my learning, and came up with this approach:
msg = ''.join([c if c in guesses else '_' for c in word])
where
guesses
contains all of the letters entered by the players so far and, of course,
word
is the word they are trying to guess.
After the above process,
found = msg == word
will indicate whether the player has guessed the word or not.