So after a lot of work I got functions to compare one word with another one. Now my problem is formatting the output. I have one function that locates letters int he wrong position, one that finds matching letters and one to compare the results in order to format the guess word. I want it to wrap correct letters in the right position in (), correct letters in the wrong spot in [], and do nothing to letters not in the word. My current program works perfectly if I don't put words with repeating letters in. Like if 'bones' is the word and I put in 'books' it causes a problem. I have spent hours on this and just can't get the comparisons right. Here's my function to find wrong positions:

def wrongPosition(computerWord, userGuess):
    # Determine what letters are in the correct position
    # make copies
    realWord= list(computerWord)

    # make copies
    guess= list(userGuess)

    # iterate based on the shortest list length
    for i in range(min(len(realWord), len(guess))): 

        # If the letter is in the list & in the correct spot, change it to a '*'
        if realWord[i] == guess[i]:
            inWordList= list(realWord[i])

            # Debug
            print(inWordList)

            # Change the letter in both to a '*'
            realWord[i] = guess[i] = '*'

        # Debug code- index value & modified lists
        print ("i = ", i)
        print (realWord)
        print (guess)

    # Create sets to eliminate extra '*'    
    wrongPosition = set(realWord) & set(guess)

    # Get rid of the reamining '*' (correct letters) so only incorrect ones will remain
    wrongPosition.discard('*')

    # Debug & return
    print(wrongPosition)
    return(wrongPosition)

and the correct letter/position function:

def inWord(computerWord, userWord):
    # Determine what letters are in the correct position
    # make copies
    realWord= list(computerWord)

    # make copies
    guess= list(userGuess)

    # iterate based on the shortest list length
    for i in range(min(len(realWord), len(guess))): 

        # If the letter is in the list & in the correct spot, change it to a '*'
        if realWord[i] != guess[i]:
            inWordList= list(realWord[i])
            #print(inWordList)

            # Change the letter in both to a '*'
            realWord[i] = guess[i] = '*'

        # Debug code- index value & modified lists
        #print ("i = ", i)
        #print (realWord)
        #print (guess)

    # Create list of letters still in the word
    matchingLetters= list(realWord)
    print(matchingLetters)
    return(matchingLetters)

and finally the function I was trying to use to compare them:

def checkWord(user, computer, wrong, remaining):
    # Convert to list
    wrongList= list(wrong)
    matching= list(remaining)
    
    # Debug
    print(wrongList)
    print(user)
    print(computer)
    print(matching)

    # Iterate based on number of letters in guess
    for letter in user:

        # Debug
       # print(letter)

        # if letter in set, it's in the wrong place
        if letter in wrongList:
            print('[', letter, ']')

        elif letter in matching:
            print('(', letter, ')')

        else:
            print(letter)

now this works if I put in like 'zores' and the words 'horse'. My output after the check function is formatted like Z(O)(R)[E][S]. But if I put in 'boons' and the word's 'bones', I get (B)(O)(O)[N](S) instead of what I want which is (B)(O)O[N](S). I know the problem is in the compound statements but I don't know what else to do; I've revised it 10+ times.

WrongPosition method is not needed here. Try with these two methods.

def inWord(computerWord, userWord):
    # Determine what letters are in the correct position
    # make copies
    realWord= list(computerWord)

    # make copies
    guess= list(userWord)

    # iterate based on the shortest list length
    for i in range(min(len(realWord), len(guess))): 

        # If the letter is in the list & in the correct spot, change it to a '*'
        if realWord[i] != guess[i]:

            # Change the letter in both to a '*'
            realWord[i] = guess[i] = '*'

    strrealword = ''
    for letter in realWord:
        strrealword += letter 
    return(strrealword)

and

def checkWord(user, computer, remaining):    

    # Iterate based on number of letters in guess
    for letter_ind in range(len(user)):

        if user[letter_ind] != computer[letter_ind] and computer.find(user[letter_ind]) != -1 and remaining.find(user[letter_ind]) == -1:
            print('[', user[letter_ind], ']')
        elif user[letter_ind] == computer[letter_ind]:
            print('(', user[letter_ind], ')')
        else:
            print(user[letter_ind])

@bawakrbs: your checking is quite nice, but it shows two 'a' in wrong place if word is
'passi' and guess is 'allas' and not s in wrong place for 'saisi'. Of course your printing should be without newlines (, in Python2, end='' in Python3)

p a s s i
[ a ] l l [ a ] [ s ]

p a s s i
k ( a ) l a

p a s s i
l [ i ] [ p ] [ a ] [ s ]

p a s s i
s ( a ) i ( s ) ( i )

Here my version:

def check_word(user, computer):    
    "Check correct letters in user which are in right place or incorrect place compared to computer word"
    user, computer = list(user), list(computer)
    for letter_ind in range(len(user)):
        if letter_ind < len(computer) and user[letter_ind] == computer[letter_ind]:
            print '(', user[letter_ind], ')',
            computer[letter_ind] = '!'
        elif user[letter_ind] in computer:
            pos = computer.index(user[letter_ind])
            if (pos > len(user) - 1) or user[pos] != computer[pos]:
                print '[', user[letter_ind], ']',
                computer[pos] = '-'
            else:
                print user[letter_ind],                
        else:
            print user[letter_ind],
    print

correct = 'passi'
for t in 'altaasi', 'keli', 'kala', 'lipas', 'saisi':
    print ' '.join(correct)
    check_word(t, correct)
    print
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.