0

I am more accustomed to C# than Python. As just something to do in my spare time I decided to make a simple hangman console application in Python. Player 1 inputs a word, player 2 then has 5 attempts to guess it. Say the word is "hello" once they make a guess, lets say "d" it would print _ _ _ _ _ . If their guess was "e" it would print _ e _ _ _ .

For some reason no matter whether I guess a correct or incorrect letter it just displays one _

word = input("Player 1, please enter a word: ")
lives = 5
print("Player 2, you have {} lives left.".format(lives))
print("The Word: ", "_ " * len(word))
wordSuccessfullyGuessed = False
while lives > 0 or wordGuessed: 
    guess = input("Player 2, guess a letter: ")
    wordFormatted = ""
    for char in word:
        if char in guess:
            wordFormatted = char + " "
        else:
            wordFormatted = "_ "

    print(wordFormatted)



Tim
  • 125
  • 2
  • 8

5 Answers5

2
word = input("Player 1, please enter a word: ").lstrip().rstrip().lower()
wordBlank = ['_ ' for char in word]
lives = 5
wordLen = len(word)
wordSuccessfullyGuessed = False
wordFormatted = ""
wordGuessed = False
while lives > 0 or wordGuessed:
    guess = ''
    validGuess = False
    print("Player 2, you have {} lives left.".format(lives))
    while len(guess) != 1 or guess.isalpha() == False:
        guess = input('Enter one letter to guess: ').lower()
        characterIteration = 0
        for char in word:
            if guess == char:
                validGuess = True
                wordBlank[characterIteration] = guess+' '
            characterIteration += 1
    if validGuess == False:
        lives -= 1
    print("The Word: ", ''.join(wordBlank))
    if '_ ' not in wordBlank:
        wordGuessed = True
if wordGuessed == True:
    print('You won! The word was indeed', word)
if wordGuessed == False:
    print("You didn't win this time, the word was",word)

Output:

Player 1, please enter a word: trees
The Word:  _ _ _ _ _
Player 2, you have 5 lives left.
Enter one letter to guess: a
The Word:  _ _ _ _ _
Player 2, you have 4 lives left.
Enter one letter to guess: e
The Word:  _ _ e e _
Player 2, you have 4 lives left.
Enter one letter to guess: t
The Word:  t _ e e _
Player 2, you have 4 lives left.
Enter one letter to guess: r
The Word:  t r e e _
Player 2, you have 4 lives left.
Enter one letter to guess: s
The Word:  t r e e s
You won! The word was indeed trees
Daniel Glynn
  • 584
  • 4
  • 9
1

On your inner for loop you are reassigning the variable wordFormatted with each iteration. It looks like you want to append the letter or the underscore to your string. Try instead:

for char in word:
        if char in guess:
            wordFormatted += char + " "
        else:
            wordFormatted += "_ "

It also appears that you are reassigning your wordFormatted = "" with each iteration of your while loop. This is going to end up clearing out the word with each guess. Might want to take a look into that too.

Josh
  • 65
  • 2
  • 8
0

As Carcigenicate mentions in a comment:

wordFormatted = "_ " looks like a likely suspect. I think you mean wordFormatted += "_ " –

using this solved the issue.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Tim
  • 125
  • 2
  • 8
0

Here

def hangman_print(guess_lst, word):
    lst = []
    for x in word:
        lst.append(x if x in guess_lst else '_')
    print(''.join(lst))


hangman_print(['l', 't'], 'telescope')

output

t_l______
balderman
  • 22,927
  • 7
  • 34
  • 52
0

You don't need to iterate over every character in the word. The builtin in when used on strings will already handle checks to see if a substring is in a string.

word = input("Player 1, please enter a word: ")
lives = 5
print("Player 2, you have {} lives left.".format(lives))
print("The Word: ", "_ " * len(word))
wordSuccessfullyGuessed = False
guessed = []
while lives > 0 or wordGuessed: 
    guess = input("Player 2, guess a letter: ")
    guessed.append(guess) # This is what you want to track, what letters they've already guessed
    wordFormatted = ''.join([letter + ' ' if letter in guessed else '_ ' for letter in word])

    print(wordFormatted)

The list comprehension I used for wordFormatted will give you the output you expect, where non-guessed chars are shown as '_ ' and correctly guessed ones, including duplicate letters, will display to the guesser.

Player 1, please enter a word: apple
Player 2, you have 5 lives left.
The Word:  _ _ _ _ _
Player 2, guess a letter: a
a _ _ _ _
Player 2, guess a letter: p
a p p _ _
Player 2, guess a letter: e
a p p _ e
Player 2, guess a letter: s
a p p _ e