1

I am trying to make a hangman game and I am not fully sure on how to add a specific letter to a variable. For example, adding a letter that someone chose through the pythons' input prompt to a variable. Here is the code I am working on:

import random
import time

word_list = ['that', 'poop', 'situation', 'coding', 'python', 'turtle', 'random', 'passive', 'neutral', 'factor', 'time']
word_chosen = random.choice(word_list)

your_name = input("What is your name?")
time.sleep(1)
print("Hello " + your_name + ", lets play some hangman!, The amount of letters in the word is below!")
guesses = 7

time.sleep(1)
hidden_word = ""
for i in word_chosen:
  hidden_word += "-"
while True:
  n = 1
  time.sleep(1) 
  print("Your word is: " + hidden_word)
  print("___________")
  print("|")
  print("|")
  print("|")
  print("|")
  print("|")
  print("|")
  print("|")
 


  characters = (word_chosen)
  time.sleep(1)
  letters_correct = ""
  characters_guessed = input("Type in the letters you think are in this word!")
  for i in characters:
    hidden_word == characters_guessed
    
  if characters_guessed == characters:
    print(hidden_word + characters)
  else:
    int(guesses) - int(n)
    print("Nope, sorry.")
Noob
  • 29
  • 4

2 Answers2

2

I have modified your code @Noob and made it work. Instead of using strings I have made lists of chosen_word ,characters and characters_guessed .In this code you can even enter more than one word at a time. It automatically fillsup the repitive words and will tell the user if he or she has won or lost the game.

import random
import time

word_list = ['that', 'poop', 'situation', 'coding', 'python', 'turtle', 'random', 'passive', 'neutral', 'factor', 'time']
word_chosen = random.choice(word_list)
your_name = input("What is your name?")
time.sleep(1)
print("Hello " + your_name + ", lets play some hangman!, The amount of letters in the word is below!")

guesses = 7

time.sleep(1)
hidden_word = [" - " for i in range(len(word_chosen))]
while guesses>0:
    time.sleep(1) 
    print("Your word is: "+ "".join(hidden_word))
    print("___________")
    for i in range(7):
        print("|")


    characters = list(word_chosen)
    time.sleep(1)
    letters_correct = ""
    characters_guessed = input("Type in the letters you think are in this word!").split()

    for j in characters_guessed:
        if j not in characters:
            guesses-=1
            print(f"Wrong guess, {j} is not in the word")
        else:
            for i in characters:
                if i==j:
                    find_position=0
                    for k in range(characters.count(i)):
                        hidden_word[characters.index(i,find_position)]=j
                        find_position+=characters.index(i,find_position)+1
        
    if " - " not in hidden_word:
        print("Congratulations, you have won the game.The word was {chosen_word}")
        break

    elif guesses==0:
        print("Opps!! out of guesses. You have lost the game.")
1

Python strings do not support item assignment, so I would use an array of strings and then use "".join(hidden_word) when you want to print the word out. You can loop over the chosen word and then when that letter matches the input letter, fill in that position in your hidden_word array.

Here's how I would adapt your code to get the game to run:

import random
import time

word_list = ['that', 'poop', 'situation', 'coding', 'python', 'turtle', 'random', 'passive', 'neutral', 'factor', 'time']
word_chosen = random.choice(word_list)

your_name = input("What is your name?")
time.sleep(1)
print("Hello " + your_name + ", lets play some hangman!, The amount of letters in the word is below!")

guesses = 7
won = False
hidden_word = ["|"]  * len(word_chosen)

while guesses > 0:
  print("Your word is: " + "".join(hidden_word))
  print("___________")
  for _ in range(5):
      print("|")

  guessed = input("Type in the letters you think are in this word!")
  for c in guessed:
    for i in range(0, len(word_chosen)):
        if word_chosen[i] == c:
            hidden_word[i] = c

  if "|" not in hidden_word:
      won = True
      break

  guesses -= 1

if won:
    print(f'Word found: {"".join(hidden_word)}')
else:
    print(f'Nope, sorry - word was {word_chosen}')

So to answer your original question, I assign the string that is input to guessed. I then loop over each letter in guessed, and check it against each position in chosen_word. If the letter at that position is a match, I change the placeholder | to the chosen letter. Once no placeholders are left, the game is won.

If you wanted to limit the user to only inputting one character at a time, I would take a look at this thread for example on how to adapt input() to handle this.

James Cockbain
  • 466
  • 2
  • 4
  • 13