Ben Dover
Ben Dover

Reputation: 163

How do you replace multiple identical characters in a string based on their positions?

So i was making a program for hangman and this is it so far,

word = input("Enter a word: ").upper()
blanks = (len(word)*"_")
print(blanks)
chances = 5
while blanks.count("_") > 0:

    letter = input("Enter a letter: ").upper()

    if len(letter) > 1:
        print("Please enter one letter only")

    if letter in list(word):
        blanks = blanks[:word.index(letter)] + letter + blanks[word.index(letter)+1:]
        print(blanks)
        continue

    else:
        chances -= 1
        if chances > 0:
            print("That letter isn't in the word, you have", chances, "chance(s) left")

    if chances == 0:
        print("You lost. The word was", word.lower())
        exit()
print("You win! The word was", word.lower())



I was just trying the code out so I would give the word to guess myself, but if I ever gave a word with a letter repeated , like "doggo".

If i gave the letter as "o", it would give out "_ O _ _ _ " instead of " _ O _ _ O" even if i guess "o" again.

Is there anyway I can fix this?

Upvotes: 2

Views: 67

Answers (3)

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

You can use bytearray and memoryview or list:

# bytearray and memoryview
blank = '_' * 5
print(blank)
fill = 'abcde'
ctr = 0
ba = bytearray(blank, 'utf-8')
mv = memoryview(ba)

while ba.count(b'_') > 0:
    mv[ctr] = ord(fill[ctr])
    ctr += 1

blank = ba.decode()
print(blank)

Output:

_____

abcde

Second approach, list:

blank = '_____'
print(blank)
blank_list = list(blank)
fill = 'abcde'

while blank_list.count('_') > 0:
    blank_list[ctr] = fill[ctr]
    ctr += 1

blank = ''.join(blank_list)
print(blank)

Output:

_____

abcde

Upvotes: 0

Mateo Lara
Mateo Lara

Reputation: 917

This worked for me:

word = input("Enter a word: ").upper()
blanks = (len(word) * "_")
print(blanks)
chances = 5
while blanks.count("_") > 0:

    letter = input("Enter a letter: ").upper()

    if len(letter) > 1:
        print("Please enter one letter only")

    if letter in list(word):
        for count, character in enumerate(word):
            if letter == character:
                blanks = blanks[:count] + letter + blanks[count + 1:]
        print(blanks)
    else:
        chances -= 1
        if chances > 0:
            print("That letter isn't in the word, you have",
                  chances, "chance(s) left")

    if chances == 0:
        print("You lost. The word was", word.lower())
        exit()
print("You win! The word was", word.lower())

The difference is that instead of making one replacement we iterate over the word to find all the coincidences and make all the necessary replacements of the same letter.

Upvotes: 1

DYZ
DYZ

Reputation: 57033

One solution is to use zip and join in a list compression. zip combines two strings pairwise. The list comprehension steps through the pairs of letters from blanks and word and compares them to the letter. join combines the result - a list of letters - back into a string.

word = "hello"
letter = 'o'
blanks = ''.join([(l if l == letter else b) for b, l in zip(blanks, word)])
#'__ll_'

Upvotes: 1

Related Questions