Python Forum
Replacing letters on a string via location
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replacing letters on a string via location
#1
Hey guys,

I was wondering if there is a way to replace a letter on a string via location, and if i can do it if there are multiple locations.
I'm working on a hangman simple python code

def game_start():
    print("Hey there, would you like to play the game? (Y/N)")
    user_answer = input("(Y/N)>>> ")
    if user_answer.lower() == "y":
        print("Thinking on a word...")
        word_random = random.choice(list_list)
        word_enumerate = list(enumerate(word_random))
        word_simulator = "_ " * len(word_random)
        word_trys = 0
        word_max = len(word_random) * 3
        # time.sleep(2)
        while word_trys < word_max:
            print("Enter letter to guess: ")
            print(word_random)
            user_answer = input(">>> ")
            if len(user_answer.lower()) > 1 or user_answer.isdigit() == True:
                print("Invalid answer")
                continue;
            else:
                word_integers = [x for x, y in word_enumerate if y == user_answer]
This is my main function,
i'm stuck on the last row, i have for example:

word_random = india
user_answer = "i"
word_integers = [0, 4]
word_simulator = _ _ _ _ _ # This is what displays each guess for the user, which i want to convert next round to i _ _ i _
thanks :)
Reply
#2
Would have to change string to a list.
string = 'india'
visible = '_' * len(string)
print(' '.join(list(visible)))

def guess(letter, string, visible):
    visible = list(visible)
    for enum, c in enumerate(string):
        if c == letter:
            visible[enum] = c

    return ''.join(visible)

visible = guess('i', string, visible)
print(' '.join(list(visible)))
or keep visible word as a list.
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Keep in mind that as strings are immutable, to replace characters you will need to rebuild the string with the replacements you want.

In that case, you might just as well rebuild word_simulator each time based on user guesses so far:
word_simulator = ''.join([c if c in user_guesses else '_' for c in word_random])
where user_guesses holds a copy of all of the unique guesses so far.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing String Variable with a new String Name kevv11 2 774 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Replacing a words' letters in a string cananb 2 3,457 Dec-01-2020, 06:33 PM
Last Post: perfringo
  Replacing characters in a string with a list cjms981 1 1,816 Dec-30-2019, 10:50 PM
Last Post: micseydel
  Select single letters from string based on separate string Jpen10 3 2,656 Dec-15-2019, 01:21 PM
Last Post: Jpen10
  getting first letters of a string Unknown_Relic 1 1,708 Nov-12-2019, 11:16 AM
Last Post: perfringo
  Counting the number of letters in a string alphabetically TreasureDragon 2 2,884 Mar-07-2019, 01:03 AM
Last Post: TreasureDragon
  Replacing all letters in a string apart from the first letter in each word Carbonix 9 4,923 Jan-17-2019, 09:29 AM
Last Post: buran
  Python Hangman Replacing "_" with letters. 2skywalkers 6 12,071 Jun-25-2018, 12:01 PM
Last Post: gruntfutuk
  Replacing variable in a split string and write new file python MyCode 1 3,557 Oct-30-2017, 05:20 AM
Last Post: heiner55
  Amount of letters in a inputted string? CyanSupreme 3 3,616 May-10-2017, 09:40 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020