Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hang man game
#1
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")
Reply
#2
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
Reply
#3
Quote:If you use the global variables
I would say: If you must use global variables, (not a good idea') ...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  multiprocess hang when certain number is used in the program esphi 7 3,172 Nov-06-2020, 03:49 PM
Last Post: esphi
  Why does this hang the system up Able98 15 9,988 Sep-02-2017, 09:50 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020