tRot
tRot

Reputation: 75

Hangman program: incorrect use of global variable in loop

I'm writing a program to play the game hangman, and I don't think I'm using my global variable correctly.

Once the first iteration of the program concludes after a correct guess, any successive iteration with a correct guess prints the word and all of its past values.

How can I only print the most current value of word? This chunk of code is within a while loop where each iteration gets user input. Thanks!

Code:

word=''

#lettersGuessed is a list of string values of letters guessed

def getGuessedWord(secretWord, lettersGuessed):
    global word
    for letter in secretWord:
        if letter not in lettersGuessed:
            word=word+' _'
        elif letter in lettersGuessed:
            word=word+' '+letter
    return print(word)

The Output:

#first iteration if 'a' was guessed:
a _ _ _ _

#second iteration if 'l' was guessed:
a _ _ _ _ a _ _ l _

#third iteration if 'e' was guessed:
a _ _ _ _ a _ _ l _ a _ _ l e

#Assuming the above, for the third iteration I want:
a _ _ l e

Note: This is only a short section of my code, but I don't feel like the other chunks are relevant.

Upvotes: 1

Views: 31

Answers (2)

ktzr
ktzr

Reputation: 1645

every time you are calling the function getGuessedWord you are adding to `word, You can not use a global:

secretWord = "myword"


def getGuessedWord(secretWord, lettersGuessed):
    word = ""
    for letter in secretWord:
        if letter not in lettersGuessed:
            word=word+' _'
        elif letter in lettersGuessed:
            word=word+' '+letter
    return print(word)


getGuessedWord(secretWord,"")
getGuessedWord(secretWord,"m")
getGuessedWord(secretWord,"mwd")

Or you can solve this by setting word at a constant length, (not as nice and harder to follow) e.g: word='_ '*len(secretWord), then instead of adding to it, replace the letter word=word[:2*i]+letter +word[2*i+1:]

Example here:

secretWord = "myword"
word='_ '*len(secretWord)

def getGuessedWord(secretWord, lettersGuessed):
    global word
    for i, letter in enumerate(secretWord):
        if letter in lettersGuessed:
            word=word[:2*i]+letter +word[2*i+1:]
    return print(word)

getGuessedWord(secretWord,"")
getGuessedWord(secretWord,"m")
getGuessedWord(secretWord,"w")
getGuessedWord(secretWord,"d")

Upvotes: 0

lmiguelvargasf
lmiguelvargasf

Reputation: 69785

The main problem you are facing is that you are appending your global variable every time you call your function. However, I think you don't need to use a global variable, in general this is a very bad practice, you can simply use the following code considering what you are explaining in your question:

def getGuessedWord(secretWord, lettersGuessed):
    return ' '.join(letter if letter in lettersGuessed else '_'
                    for letter in secretWord)

I also think that it is better if you use a python comprehension to make your code faster.

Upvotes: 1

Related Questions