I have been working on this program for a few days and have most of it working, the only issue that I am having is when a word has multiple iterations pf the same letter. I need the program to print out all the letters that are the same when they are guessed. example if the word is apple and the user guesses p then I need both letter p's to be printed on the output line. Any help would be greatly appreciated, here is what I have.

import random

#Program 4  Hangman

#print the hangman partition
def hangman(turn, correct):
    if correct > 0:
        turn = turn - correct

    if turn == 1:
        print('|')
    elif turn == 2:
        print('|')
        print('O')
    elif turn == 3:
        print(' |')
        print(' O')
        print('/')
    elif turn == 4:
        print(' |')
        print(' O')
        print('/|')
    elif turn == 5:
        print(' |')
        print(' O')
        print('/|\\')
    elif turn == 6:
        print(' |')
        print(' O')
        print('/|\\')
        print('/')
    elif turn == 7:
        print(' |')
        print(' O')
        print('/|\\')
        print('/ \\')


#function to play the hangman game 
def game(word):

    word = word.upper() #capitalize the word 
    print(word)
    hint = "-" * len(word)  #setup the for the hidden word
    count = 0   # counter for the while loop for the 7 user inputs
    wrong = []
    number = 0
    badGuess = 1
    correctguess = 0
    i = 1
    while (count == 0):
        print (hint)

        guess = input('Please enter guess # ' + str(i) + ':')
        guess = guess.upper()   #capitalize the user inputs

        #loop to see if the guess is in the hidden word
        if guess in word:   
            print('That letter is in the word')
            index = word.find(str(guess))

            x = list(hint)
            x[index] = guess
            hint = "".join(x)
            correctguess += 1
            hangman(i, correctguess)


        # if guess is not in the hidden word go to the function to draw hangman
        else:           
            hangman(i, correctguess)
            wrong.append(guess)
            w = " ".join(wrong)
            print ('The incorrect letters you have guessed: ' + w)
            badGuess += 1

        if hint == word:
            print('Congratulations you have guessed the word.')
            break
        elif badGuess > 7:
            print('Sorry you took too many guesses that the word was ' + word)
            break
        i += 1
#open the file and read it into a list
file= open('words.txt')
word = file.readlines()

#select a word at random for the game and pass that word to the function game
select = random.choice(word)
selection = select.rstrip()

game(selection)

Recommended Answers

All 3 Replies

You can pass an optional start location to .find() but that means iterating over the entire list several times. Another way

word="APPLE"
guess="P"
hint=["-" for ctr in range(len(word))]
print "initial =", hint
for index in range(len(word)):
    if word[index]==guess:
        hint[index]=guess

print "".join(hint)

You can also eliminate some of the redundancy when printing the hangman

def print_hangman(turn):
    print("turn=%d" % (turn))
    if turn >= 1:
        print(' |')
    if turn >= 2:
        print(' O')
    if turn >= 3:
        print('/', end="")
    if turn >= 4:
        print('|', end="")
    if turn >= 5:
        print('\\')
    if turn >= 6:
        print('/', end="")
    if turn == 7:
        print(' \\')
    print(" ")

for ctr in range(1, 8):
    print_hangman(ctr)
    input()

great, that is exactly what I was looking for.
Thank you guys very much

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.