Nellington
Nellington

Reputation: 221

Python - Word guessing game fails on words with repeat characters

I'm new to programming, but I've been teaching myself Python on and off for a few months. I've written a word guessing game program. The program does the following things:

Here's the code:

import random

dictionary = ("CROWS", "GOAT", "MONKEY", "COW", "HORSE", "SNAKE")
answer = random.choice(dictionary)
answer_length = len(answer)
victory = True
print("""
    I have chosen a word.  Your job is to guess it. I will help you
    keep track.
    """)

print("The word I have chosen is {} letters long.\n".format(answer_length))

underscores = []
for character in answer:
    underscores.append("_")
print(underscores)

while victory:
    guess = input("\nPlease guess a letter: ").strip().capitalize()
    def pos(guess):
        position = 0
        for letter in answer:
            if letter != guess:
                position += 1
            else:
                print("Correct!")
                break
        return position

    def update_board(x):
        global underscores
        if guess in answer:
            underscores[x] = guess
        else:
            print("This letter is not in the word")
        return underscores

    def winner():
        global underscores
        global victory
        if "_" not in underscores:
            print("YOU HAVE WON!")
            victory = False

    x = pos(guess)
    print(update_board(x))
    winner()

Now, the problem I'm having is this: The program works fine, provided that the word the computer chooses from the dictionary doesn't have any repeated letters in it. You'll notice that the words in the dictionary are all comprised of unique characters. If, however, the dictionary included the word 'CHICKEN', the program could run into problems.

Imagine the computer chose 'CHICKEN', and then you guessed the letter 'C'. The program would return:

Correct!
["C", "_", "_", "_", "_", "_", "_"]

Moreover, if you guessed C again, it would just print

Correct!
["C", "_", "_", "_", "_", "_", "_"]

again. Ideally, what I want it to do is return ["C", "_", "_", "C", "_", "_", "_"] the very first time the user guessed 'C'. At the moment, the game can only work if all the words in the dictionary don't have any recurring characters.

Any help you could provide would be much appreciated. Thanks!

Upvotes: 1

Views: 1558

Answers (2)

legend-is-back
legend-is-back

Reputation: 443

you can use array to solve it :

import random

dictionary = ("CROWS","CHICKEN")
answer = random.choice(dictionary)
answer_length = len(answer)
victory = True
print("""
    I have chosen a word.  Your job is to guess it. I will help you
    keep track.
    """)

print("The word I have chosen is {} letters long.\n".format(answer_length))

underscores = []
for character in answer:
    underscores.append("_")
print(underscores)
ar=[]
while victory:
    guess = input("\nPlease guess a letter: ").strip().capitalize()
    def pos(guess):
        position = 0
        for letter in answer:
            if letter != guess:
                position += 1
            else:
                print("Correct!")
                ar.append(position)
                position += 1

        return ar

    def update_board(x):
        global underscores
        if guess in answer:
            underscores[x] = guess
        else:
            print("This letter is not in the word")
        return underscores

    def winner():
        global underscores
        global victory
        if "_" not in underscores:
            print("YOU HAVE WON!")
            victory = False

    pos(guess)
    for x in ar:
        print(update_board(x))
    del ar [:]
    winner()

Upvotes: 1

Roope
Roope

Reputation: 4507

You're stopping to check for any further occurrences once you find one in pos(guess). Thus, you only get the first occurrence. Obviously then, to solve this, don't stop when you find something. Instead go through the word fully every time.

E.g. in pos(guess), return a list of indices that the letter is found at in the word, and in update_board(x) loop through the indices you get instead of replacing only the one underscore with the guess.

Upvotes: 2

Related Questions