Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this function result in ['h', '-', '-', '-', '-']?

The function hangman_guessed(guessed, secret) is supposed to take a string of guessed characters and a list of "secret" characters.

The function checks every character in the secret list and compares it with each character in the guessed character string to check if the character is in both. If the characters are not the same then the function places a - in a temporary list equal to the secret list (so that we can still compare other characters in the guessed list to the original secret list later).

def hangman_guessed(guessed, secret):
    modified = secret
    for i1 in range(len(secret)):
        for i2 in range(len(guessed)):
            if secret[i1] == guessed[i2]:
                modified[i1] = secret[i1]
                break
            else:
                modified[i1] = '-'
    return modified

For example, when I run hangman_guessed('hl', ['h','e','l','l','o']), it should return ['h', '-', 'l', 'l', '-'], but currently it returns ['h', '-', '-', '-', '-'].

The problem here is that only the first character in the guessed list is considered, but I do not know why. It this case, it is expected that the program checks over the 'l' characters in ['h','e','l','l','o']) and sets the corresponding characters in the temporary list modified to -, but to my understanding after the for loop runs again and checks the original secret list for l characters it should overwrite the - in the modified list and the result should have the 'l' characters rather than the - characters.

like image 759
JohnDoe Avatar asked Feb 05 '26 16:02

JohnDoe


1 Answers

A list-comprehension is perfectly suited to what you want to do. We want to create a list of each character (let this be i) in secret if i is in guessed else we want to have a hyphen ("-").

def hangman_guessed(guessed, secret):
    return [i if i in guessed else "-" for i in secret]

and a test to show it works:

>>> hangman_guessed('hl', ['h','e','l','l','o'])
['h', '-', 'l', 'l', '-']

As you get more used to the flow of Python, you will find that comprehensions in general are extremely useful as well as being very readable for a whole variety of things.


If for some reason however, you had to use nested for-loops and weren't allowed to use the really simple in operator, then you need to / can make a couple of corrections to your current code:

  • make a copy of the secret list first
  • iterate through the characters in guessed, rather than the indexes

After making these two corrections, the function will look something like:

def hangman_guessed(guessed, secret):
    modified = secret[:]
    for i in range(len(secret)):
        for g in guessed:
            if secret[i] == g:
                modified[i] = secret[i]
                break
            else:
                modified[i] = '-'
    return modified

which now works:

>>> hangman_guessed('hl', ['h','e','l','l','o'])
['h', '-', 'l', 'l', '-']
like image 145
Joe Iddon Avatar answered Feb 07 '26 05:02

Joe Iddon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!