Fufu Alex
Fufu Alex

Reputation: 37

Replace letter in word

If the guess is correct it will replace the letter with '-'. in the for loop how can i store the previous result and do a update with the new result?

import random

WORDS = ('linux', 'windows')
correct_word = random.choice(WORDS)


for n in range(5):
    guess = input('Enter a letter: ')
    letter = ''.join(x if x in guess else '-' for x in correct_word)

    if letter in correct_word:
        print("So far you have: ", letter)

    else:
        print("So far you have: ", letter)

Upvotes: 0

Views: 296

Answers (4)

Chau Giang
Chau Giang

Reputation: 1554

Here is quick draft for you, I strong believe that you can implement your fully idea:

import random

WORDS = ('linux', 'windows')
correct_word = random.choice(WORDS)

question = list( '-' * len(correct_word))
for n in range(5):
    guess = input('Enter a letter: ')
    for index, letter in enumerate(correct_word):
        if letter is guess:
            question[index] = letter
    print("So far you have: ", ''.join(question))

Upvotes: 0

Phydeaux
Phydeaux

Reputation: 2855

Try keeping the letters guessed so far in a variable, like this (I removed the if statement because both branches do the same thing). Also added input validation:

import random

WORDS = ("linux", "windows")
correct_word = random.choice(WORDS)

def get_single_letter_input():
    while True:
        guess = input("Enter a letter: ")
        if len(guess) == 1:
            return guess

word_so_far = "".join("-" for letter in correct_word)
for n in range(5):
    guess = get_single_letter_input()
    word_so_far = "".join(x if x in guess else word_so_far[i]
                          for i, x in enumerate(correct_word))

    print(f"So far you have: {word_so_far}")

Upvotes: 1

Nikandr Marhal
Nikandr Marhal

Reputation: 1470

Just keep your guessed word in a variable. Also I would recommend not to use fixed range as you do, but get the length of chosen word

def guess():
    words = ('linux', 'windows')
    correct_word = random.choice(words)
    guessed_word = ["-" for letter in correct_word]
    for n in range(len(correct_word)):
        guess = input('Enter a letter: ')
        for i in range(len(correct_word)):
            if correct_word[i] == guess:
                guessed_word[i] = guess
        print("So far you have: ", "".join(x for x in guessed_word))

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521259

You could try doing a regex replacement with re.sub. This answer assumes that you would be OK with using correct_word itself to maintain the state, by being updated with dashes for each matching letter which the user might choose.

import random

WORDS = ('linux', 'windows')
correct_word = random.choice(WORDS)

for n in range(5):
    guess = input('Enter a letter: ')

    if len(guess) > 1 or len(guess) == 0
        print("Please choose a single letter only")

    if letter in correct_word:
        print("Correct choice")
        correct_word = re.sub(guess, '-', correct_word)

    else:
        print("Letter not present")

Upvotes: 0

Related Questions