How are you guys doing today? I'm having a hard time with my CS project. I have to write a hangman python code but it is kinda different from what I've seen around. I'll post my code and then explain what I am having trouble with:

from hangman_support import *

def isalpha(s):
    if s.isalpha():
        return True
    return False

def print_word(word1, wordbox):
    temp_word = ""
    for c in word1:
       temp_word += c + " "
    wordbox.setText(temp_word)

def main():
        win = GraphWin('Hangman', WINSIZE, WINSIZE)
        headpoint = draw_gallows(win, WINSIZE)
        wordbox = draw_word_box(win, WINSIZE)
        entrybox = draw_entry_box(win, WINSIZE)
        messagebox = draw_message_box(win, WINSIZE)
       
        try:
            s = 'hangman.txt'
            infile = open(s, "r")
            s.isalpha()
            data = readfile1(infile)
            infile.close()
            
            data = data.split()
            word1 = data[0]

            blank_word = ""
          
            for c in word1:
               blank_word += ('_')
            print_word(blank_word, wordbox)

            guessed = ""
            
            for s in word1:
                wait_for_guess(win, WINSIZE)
                guess = entrybox.getText()
                guessed += guess
                messagebox.setText(guessed)
                draw_head(win, headpoint)
                print('guess: ', guess)

                wait_for_guess(win, WINSIZE)
                guess = entrybox.getText()
                guessed += guess
                messagebox.setText(guessed)
                draw_body(win, headpoint)
                print('guess: ', guess)

                wait_for_guess(win, WINSIZE)
                guess = entrybox.getText()
                guessed += guess
                messagebox.setText(guessed)
                draw_limb(win, headpoint, True, True)
                print('guess: ', guess)

                wait_for_guess(win, WINSIZE)
                guess = entrybox.getText()
                guessed += guess
                messagebox.setText(guessed)
                draw_limb(win, headpoint, False, True)
                print('guess: ', guess)

                wait_for_guess(win, WINSIZE)
                guess = entrybox.getText()
                guessed += guess
                messagebox.setText(guessed)
                draw_limb(win, headpoint, True, False)
                print('guess: ', guess)


                wait_for_guess(win, WINSIZE)
                guess = entrybox.getText()
                guessed += guess
                messagebox.setText(guessed)
                draw_limb(win, headpoint, False, False)
                messagebox.setText('You Lose!')
                win.getMouse()
                win.close()
            
          
        except IOError:
            print('Error! Could not open file.')

def readfile1(infile):
        data = infile.read()
        return data

main()

I am having problems on filling the secret word when the user's guess is correct. I have no idea how to do that in this code, can you guys please help me? Thank you in advance,

BWall

I prefer using a list.

word1 = "hangman"
blank_word = []
for c in word1:
    blank_word.append('_')
print blank_word

for guess in ["a", "b", "n"]:
    print("testing", guess)
    for ctr in range(len(word1)):
        if guess == word1[ctr]:
            blank_word[ctr]=guess
    print(" ".join(blank_word))

For me little simpler is to keep set of guessed letters:

word1 = "hangman"
guesses = set()

for guess in "abn":
    print("testing", guess)
    guesses.add(guess)
    print(" ".join(c if c in guesses else '_' for c in word1))

Instead of all the individual calls to wait_for_guess you can use a loop and counter like this pseudo-code example.

# all of the code like the following is redundant
            for s in word1:
                wait_for_guess(win, WINSIZE)
                guess = entrybox.getText()
                guessed += guess
                messagebox.setText(guessed)
                draw_head(win, headpoint)
                print('guess: ', guess)
#
# instead use
def check_letter(ltr, word1, blank_word):
    print("testing", ltr)
    found = False
    for ctr in range(len(word1)):
        if guess == word1[ctr]:
            blank_word[ctr]=guess
            found = True
    guesses_so_far = "".join(blank_word)
    print guesses_so_far
    if guesses_so_far == word1:
        print "You guessed it"

    return found, blank_word


blank_word = ["_" for x in range(len(word1))]
total = 6
print "You get %d incorrect guesses" % (total)
ctr = 0
while ctr < total:
    wait_for_guess(win, WINSIZE)
    guess = entrybox.getText()
    result, blank_word = check_letter(guess, word1, blank_word)
    guessed += guess
    messagebox.setText(guessed)
    if not result:  ## incorrect guess
        ctr += 1
        if 1==ctr:
            draw_head(win, headpoint)
        elif 2==ctr:
            draw_body(win, headpoint)
        ## etc

You will now have to test for the word being found before all attempts are exhausted and if so, exit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.