Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if two items in a list are the same?

Tags:

python

regex

My Objective:

In a game of Lingo, there is a hidden word, five characters long. The object of the game is to find this word by guessing, and in return receive two kinds of clues: 1) the characters that are fully correct, with respect to identity as well as to position, and 2) the characters that are indeed present in the word, but which are placed in the wrong position. Write a program with which one can play Lingo. Use square brackets to mark characters correct in the sense of 1), and ordinary parentheses to mark characters correct in the sense of 2)

Current Code:

def lingo():
    import random
    words = ['pizza', 'motor', 'scary', 'motel', 'grill', 'steak', 'japan', 'prism', 'table']
    word = random.choice(words)
    print word
    while True:
        guess = raw_input("> ")
        guess = list(guess.lower())
        word = list(word)
        for x in guess:
            if x in word:
                if x == word[word.index(x)]:
                    guess[guess.index(x)] = "[" + x + "]"
                else:
                    guess[guess.index(x)] = "(" + x + ")"
        print guess

lingo()

As of now, if the words share a common letter, it puts the letter in square brackets, regardless of whether it shares the same pos or not.

Examples:

CORRECT: - Word: Table - My Guess: Cater - OUTPUT: C[a](t)(e)r

INCORRECT: - Word: Japan - My Guess: Ajpan (Notice the switch between the a and j, I did that on purpose). - OUTPUT: [A][j][p][a][n] (Should Be (a)(j)[p][a][n])

like image 924
Brian Gunsel Avatar asked Jan 06 '23 13:01

Brian Gunsel


1 Answers

Your error is this line:

if x == word[word.index(x)]:

which is always true since word[word.index(x)] is the same thing as x. Try changing it to:

if x == word[guess.index(x)]:
like image 113
Selcuk Avatar answered Jan 19 '23 21:01

Selcuk