Python Forum
Python Hangman Replacing "_" with letters.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Hangman Replacing "_" with letters.
#1
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.
Reply
#2
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))
Reply
#3
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.
Reply
#4
(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
....
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
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.
Reply
#6
(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.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#7
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.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  eliminating letters when typed in python window deebo 0 1,706 Jan-05-2021, 09:26 AM
Last Post: deebo
  Replacing a words' letters in a string cananb 2 3,404 Dec-01-2020, 06:33 PM
Last Post: perfringo
  Python Hangman Game - Multiple Letters Problem t0rn 4 4,533 Jun-05-2020, 11:27 AM
Last Post: t0rn
  Hangman metro17 4 2,970 Sep-18-2019, 10:59 AM
Last Post: perfringo
  Trouble coding hangman Tay 1 2,330 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Hangman code problem KrakowKid 1 2,371 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Python hangman help A1395 11 6,969 Feb-13-2019, 04:24 PM
Last Post: ichabod801
  Replacing all letters in a string apart from the first letter in each word Carbonix 9 4,831 Jan-17-2019, 09:29 AM
Last Post: buran
  Hangman 2skywalkers 3 60,451 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Replacing letters on a string via location Owenix 2 2,438 Sep-16-2018, 10:59 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020