Python Forum

Full Version: Hang man game
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, beginner here.
Doing a hangman game and i am stuck because when i run my function guess_me, the return value is always new. how am i able to store this value into somewhere so that when the function runs again it will be updated?

current code prints out:
a_ _ _ _
_ p p _ _

desired code:
a_ _ _ _
app_ _

Many thanks in advance

secret_word = 'apple'
guesses = []
def guess_me(secret_word, letters_guessed):
    word_print = ''
    for i in secret_word:
        if i in letters_guess:
            word_print += i
        else:
            word_print += '_ '
    return word_print
x = 6
while x>0:
    letters_guess = input("enter: ")
    hmm = guess_me(secret_word, letters_guess)
    if letters_guess in secret_word:
        guesses.append(letters_guess)
        print('good guess')
    else:
        print("bad one")
Hello, I needed to modify the code a bit, since the code you posted doesn't print the (partial) word out.
In line 6 you need to change letters_guess to guesses. Many variables with rather similar names can cause some confusion.
If you use the global variables secred_word and guesses, you don't really need the parameters in guess_me() function. If you had a different design/strategy in mind, the code as is now probably doesn't implement it.

Edit:
Best advice is in the next post by Larz60+, keep it in mind
Quote:If you use the global variables
I would say: If you must use global variables, (not a good idea') ...