The problem I have with this Hangman Program is that it can't display all the occurrences of a letter in a string and I can't update the word so it adds the letters from the previous guess.
# Hangman.py
import random
word_bank = """astounding psychology crazed
prodigy jargon internship intelligence fundamentals
analyst dictionary ransomware facility
antelope dinosaur""".split()
alphabet = "abcdefghijklmnopqrstuvwxyz"
drawing = ["""
+--+
|
|
|
=== """, """
+--+
O |
|
|
=== """, """
+--+
O |
| |
|
=== """, """
+--+
O |
/| |
|
=== """, """
+--+
O |
/|\ |
|
=== """, """
+--+
O |
/|\ |
/ |
=== """, """
+--+
O |
/|\ |
/ \ |
===
"""]
words_in_bank = len(word_bank) - 1
random_num = random.randint(0,words_in_bank)
word = word_bank[random_num]
tries = 0
underscore = "_ " * len(word)
guess = word
def add_letter(letter, guess, tries):
while True:
if letter in guess:
index = guess.find(letter)
if index != 0:
indexs = index * 2
guesses = underscore[ :indexs] + letter + underscore[indexs+1: ]
guess = guess[ :index] + guess[index+1: ]
else:
break
return guesses
print("HANGMAN ------")
while True:
print(drawing[tries])
print(underscore)
letter = input("Enter a letter: ")
if letter in alphabet and len(letter) == 1:
if letter in word:
updated_word = add_letter(letter, guess, tries)
print(updated_word)
else:
tries += 1
print("Wrong :( ")
else:
print("You must enter 1 letter from the alphabet! \n")
continue
If let's say the word was "analyst", I want the output to be "a _ a _ _ _ _" rather than "_ _ a _ _ _ _". I also want the result to be updated when I enter the correct letter: "a n a _ _ _ _" rather than "_ n _ _ _ _ _".
I tried to change where I returned the value guesses. However, the code still does not function properly.
The problem I have with this Hangman Program is that it can't display all the occurrences of a letter in a string and I can't update the word so it adds the letters from the previous guess.
# Hangman.py
import random
word_bank = """astounding psychology crazed
prodigy jargon internship intelligence fundamentals
analyst dictionary ransomware facility
antelope dinosaur""".split()
alphabet = "abcdefghijklmnopqrstuvwxyz"
drawing = ["""
+--+
|
|
|
=== """, """
+--+
O |
|
|
=== """, """
+--+
O |
| |
|
=== """, """
+--+
O |
/| |
|
=== """, """
+--+
O |
/|\ |
|
=== """, """
+--+
O |
/|\ |
/ |
=== """, """
+--+
O |
/|\ |
/ \ |
===
"""]
words_in_bank = len(word_bank) - 1
random_num = random.randint(0,words_in_bank)
word = word_bank[random_num]
tries = 0
underscore = "_ " * len(word)
guess = word
def add_letter(letter, guess, tries):
while True:
if letter in guess:
index = guess.find(letter)
if index != 0:
indexs = index * 2
guesses = underscore[ :indexs] + letter + underscore[indexs+1: ]
guess = guess[ :index] + guess[index+1: ]
else:
break
return guesses
print("HANGMAN ------")
while True:
print(drawing[tries])
print(underscore)
letter = input("Enter a letter: ")
if letter in alphabet and len(letter) == 1:
if letter in word:
updated_word = add_letter(letter, guess, tries)
print(updated_word)
else:
tries += 1
print("Wrong :( ")
else:
print("You must enter 1 letter from the alphabet! \n")
continue
If let's say the word was "analyst", I want the output to be "a _ a _ _ _ _" rather than "_ _ a _ _ _ _". I also want the result to be updated when I enter the correct letter: "a n a _ _ _ _" rather than "_ n _ _ _ _ _".
I tried to change where I returned the value guesses. However, the code still does not function properly.
Share Improve this question asked Mar 17 at 4:15 LunaLuna 1 1 |1 Answer
Reset to default 0You have a number of problems here. For one, you never use updated_word
, and I think you are confused about how underscore
is being used. Rather than pick a random number, you can just have random.choice
pick one for you. Also, you never check for a win or a loss, and you don't check whether a letter has already been guessed.
I've redone your main loop here. This seem to work:
word = list(random.choice(word_bank))
underscore = ["_"] * len(word)
def add_letter(letter, word, underscore):
for i,c in enumerate(word):
if c == letter:
underscore[i] = c
print("HANGMAN ------")
tries = 0
guessed = []
while underscore != word and tries < 6:
print(drawing[tries])
print(' '.join(underscore))
if guessed:
print('\nAlready guessed:', ' '.join(guessed))
letter = input("Enter a letter: ")
if not(letter in alphabet and len(letter) == 1):
print("You must enter 1 letter from the alphabet!")
elif letter in underscore:
print("You already guessed that letter.")
elif letter in word:
add_letter(letter, word, underscore)
guessed.append(letter)
guessed.sort()
print("Yes...")
else:
tries += 1
guessed.append(letter)
guessed.sort()
print("Wrong :( ")
if tries == 6:
print("You lose!")
else:
print("You WIN!")
input
, randomness etc...) – Julien Commented Mar 17 at 4:31