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.