Home > Software design >  Python Hangman how to insert the right letter in the word?
Python Hangman how to insert the right letter in the word?

Time:05-25

Ok so I am trying to insert the letter in the random word that was generated. So for example if the user has picked the right letter in the word how to I make it pop up in the word instead of the str "_".

import random





print("Welcome to Hangman Enter A Letter")


random_words = ['abnegation', 'able', 'aborning', 'Abigail', 'Abidjan', 'ablaze', 'abolish', 'abbe', 'above', 'abort', 'aberrant', 'aboriginal', 'aborigine', 'Aberdeen', 'Abbott', 'Abernathy', 'aback', 'abate', 'abominate', 'AAA', 'abc', 'abed', 'abhorred', 'abolition', 'ablate', 'abbey', 'abbot', 'Abelson', 'ABA', 'Abner', 'abduct', 'aboard', 'Abo', 'abalone', 'a', 'abhorrent', 'Abelian', 'aardvark', 'Aarhus', 'Abe', 'abjure', 'abeyance', 'Abel', 'abetting', 'abash', 'AAAS', 'abdicate', 'abbreviate', 'abnormal', 'abject', 'abacus', 'abide', 'abominable', 'abode', 'abandon', 'abase', 'Ababa', 'abdominal', 'abet', 'abbas', 'aberrate', 'abdomen', 'abetted', 'abound', 'Aaron', 'abhor', 'ablution', 'abeyant', 'about']




random_choice = random.choice(random_words)


length_of_word = len(random_choice)



for x in range(1, length_of_word   1):
    print("_ ", end='')


user_letter = input("PICK A LETTER\n")


if user_letter in random_choice:
    True

CodePudding user response:

I would solve the problem like this:

for letter in word_to_guess:
        if letter in already_guessed:
            print(letter, end="")
        else:
            print("_", end="")

CodePudding user response:

First off, I would recommend making your random_words list only two or three words, just for ease of debugging.

The easiest way to solve this would be to rethink you logic. Instead of printing out _ for the length of the word, create an array/list that consists only of _ that is as long as the word is. Then, create a dictionary that maps each letter in the word to its location(s).

random_choice = "apple"
length_of_word = len(random_choice)
letter_to_location = {}
for i in random_choice: # This loops creates the dictionary
    if random_choice[i] in letter_to_location: # if we already saw this letter
        letter_to_location[random_choice[i]].append(i) # append to our list of locations
    else: # otherwise make a new list of locations!
        letter_to_location[random_choice[i]] = [i]

guesser = ["_" for i in range(length_of_word)]
print(guesser) # This prints _ _ _ _ _

user_guess = input() # let us pretend this is 'p'

if user_guess in letter_to_location: # check if the letter guessed is in our location mapping
   for loc in letter_to_location: # if it is, iterate through locations
        guesser[loc] = user_guess # update the locations with the letter
   del letter_to_location[user_guess] # delete the key

print(guesser) # this prints _ p p _ _

This is neither the best nor the fastest way to do it, but I think the mistakes you are making mean you are still new to python (or maybe code in general), so this approach should help you with working out the basics of how a program like this should run.

Once you are comfortable with this approach (or any approach that works for you), I would look into creating functions to make code snippets easier to use, implementing a while loop to run the game for the proper amount of time, and maybe default dictionaries if you are sticking with this implementation. If you aren't a fan of dictionaries, you can always try and use sets, which are faster!

  • Related