So for my intro to programming class our final project is designing a simple game in Python. i picked Lingo but I'm running into a huge problem. Lingo is played by the computer picking a random 5 letter word (from a .txt file) then the user guessing 5 times until either they run out of guesses or get the word. It's supposed to display specific keys to help the user guess the word and this is where I'm running into problems.I've looked around the web and seen posts regarding this game using sets, zip(), etc. but we never did that in class so I have no idea how to use them. I'd be more then willing to try them if someone would be kind enough to explain them to me :). But anyways, I built my core function for the program and it works except for if the user enters a word like 'books' when the actual word is 'bones'. It picks up 'o' is in the word but can't recognize there are 2 'o's in 'books' but only one in 'bones'. So for ouput i get (B)(O)[O]K(S) instead of (B)(O)OK(S). I have no idea how to tackle this issue...any help is greatly appreciated!

if guess != realWord:
        for char in guess:
            if char in realWord: # It doesnt work if the letter repeats ?
                if guess[0+indexValue] == realWord[0+indexValue]:
                    indexValue+=1
                    print('(', char,')')# If in correct position wrap in '()'
                else:
                     indexValue+=1 
                     print('[',char,']')# If in wrong position wrap in '[]'
            else:
                indexValue+=1
                print(char)# If not in word print letter alone

And what happens when they guess "books" and it is "bones". You have to eliminate a letter that is found or it will "find" two o's when there is only one. Since we don't know what kind of containers realWord and guess are, I am using a list since they are easier to work with.

real_word = "books"

for guess in ["bones", "good", "bookkeeper"]:
    print "\n  testing", guess
    if guess != real_word:
        ## list of "_" to be updated with letters found in both words
        guess_list = ["_" for x in range(len(real_word))]
        real_list = list(real_word.lower())
        for char in guess:
            if char in real_list:
               offset = real_list.index(char) ## offset position of the found letter
               guess_list[offset] = char      ## update with correct letters
               real_list[offset] = "*"        ## letter found so don't test it again
            else:
                print(char)# If not in word print letter alone

    print guess_list
    print real_list, "= real_list"  ## for testing

Edited 4 Years Ago by woooee: n/a

Ok so now I have this:

realWord= 'BONES'
guessWord=input('Enter a five letter word: ')

def checkGuess(input, word):
    guess= input.upper()

    # Proceed is the guess doesn't equal the actaul word
    if guess != word:

        # Create 5 blank spaces
        guessList= ['_' for x in range(len(word))]
        print(guessList)

        # List for the actual word
        realList= list(word)

        # Iterate for each letter in the guess word
        for char in guess:

            # Proceed if the letter is in the list for the real word
            if char in realList:

                # Name a variable to change the letters to '*' if
                # the letter from the guess is in the word
                offset= realList.index(char)
                guessList[offset]= char
                realList[offset]= '*'

            # Print characters NOT in word
            else:
                print(char)

        # List from guess with correct placement
        print(guessList)

        # Debug code
        print(realList)

    # If the user guesses right 
    else:
        print('You won!')

checkGuess(guessWord, realWord)

but now how do I format the output? Like I want it to print like this:

guess= sonbe
word= bones
output= [s][o][n][e]

indicating all the letters are in the word but in the wrong spot.
Also if I were to type boxse as my guess I would get (b)(o)x[s][e] as output where the ()= correct word, correct index; []= correct letter wrong index and nothing means its not in the word? Thank you everyone for your help!

This article has been dead for over six months. Start a new discussion instead.