Hangman Game Issues with Printing _'s??? Logic Problem, I think

Viewed 25

Here is my function that I have written so far.

alreadyGuessed = []
def changeCharToUnderScore(eachLetter):
stringDisplay = ""
for x in eachLetter:
    stringDisplay += x
    stringDisplay += " "
    print (stringDisplay)
    print("Already guessed: " + ', '.join(alreadyGuessed))

When this prints my hangman array, it prints something like this

_ )

_ _)

_ _ _)

_ _ _ _)

_ _ _ _ _)

Instead of printing

_ _ _ _ _) I am using the ")" because the underscores by themselves change the formatting in here

I have tinkered with this and I can get it to print only one, but it prints the first letter and only that. I understand I have some sort of logic error but I have had a really difficult time figuring out where I am going wrong here.

Here is my full hangman code currently, but it's a work in progress.

import time
from sys import stdout
import random as ran
wordList = ["potato", "tomato", "ramen", "moana", "disney", "veil", "space", "bowie", "russia", "chair", "couch", "glasses", "orange", "apple", "carrot", "bread", "head", "beer", "pasta", "soda", "pizza", "eggs", "noodle", "coffee", "soup", "feet", "hands", "ears", "hoodie", "pencil", "sorbet", "juice", "fan", "pan", "cup", "boba", "cheese", "chair", "purse", "knife", "spoon", "steak", "netflix", "lemon", "grape", "weed", "phone", "tire", "liar", "bench", "thirst"] # Dictionary
alreadyGuessed = [] # alpha characters already chosen
word = wordList[ran.randint(0, len(wordList) - 1)]
stringSplit = list(word) # Creates an array from the characters within the string.
stringInput = ["_"] * len(stringSplit)



def guessInput():
    printWordHint = ("The word is " + str(len(word)) + " letters long.\n")
     # Causes a delay inbetween each character being printed so that is creates the illusion of typing.
    for char in printWordHint:
        stdout.write(char)
        stdout.flush()
        time.sleep(0.03)
    return input("Guess a letter!")

### Have had issues getting this loop to print only the final array
# Prints the guessed characters, without the array brackets.
def changeCharToUnderScore(eachLetter):
    stringDisplay = ""
    for x in eachLetter:
        stringDisplay += x
        stringDisplay += " "
        print (stringDisplay)
        print("Already guessed: " + ', '.join(alreadyGuessed))

#replace hidden character from "_" to the "alpha character".
def replaceUnderScore(split, userInp):
    inputForGuess = guessInput()
    alreadyGuessed.append(inputForGuess)
    changedCharacter = True
    for x in range(len(split)):
        if inputForGuess == split[x]:
            userInp[x] = stringSplit[x]
            changedCharacter = False
    return(userInp, changedCharacter)

# Starting Game and calling Functions
changeCharToUnderScore(stringInput)
amtOfWrongGuesses = 0
correctGuess = True


while(not("_" not in stringInput or amtOfWrongGuesses >= 7)):
    stringInput, correctGuess = replaceUnderScore(stringSplit, stringInput)
    if not correctGuess:
        amtOfWrongGuesses += 1
        print("Oh no, that is not in this word!")
    changeCharToUnderScore(stringInput)
if(amtOfWrongGuesses >= 7):
    print("Blast Off! You LOSE! \nThe correct word was " + word)


else:
    print("Congratulations! You prevented the MoonMans death!")
    # winningString = ("Congratulations! You prevented the MoonMans death!")

I'm also not sure how to get my game to loop, if they want to play again.

2 Answers

You should print the resulting string outside the for loop, when it has finished concatenating the strings:

def changeCharToUnderScore(eachLetter):
    stringDisplay = ""
    for x in eachLetter:
        stringDisplay += x
        stringDisplay += " "
    print (stringDisplay)
    print("Already guessed: " + ', '.join(alreadyGuessed))
print (stringDisplay)

needs to be pulled out of the for loop.

And if you want to allow the game to be replayed, you can nest another while loop with a type of user input at the end of the game.

Related