how to include letters in my list in python and inform the user that it has been repeated

Asked

Viewed 30 times

0

for letra in palavra_secreta:
    #print(inclui_Letras)
    if (chute == letra):
        letras_acertadas[index] = letra
        inclui_Letras.append(letra)  # incluir o 'chute' na list 'inclui_letras
        print(palavra_secreta, chute, letra, inclui_Letras)
    index += 1

I am using this code to check if there is a letter placed by the user in a word. However, let’s assume that my word is 'Strawberry' and I put the letter A. In this case it is all right, but... I repeat the letter A several times and I want to inform the user (print) that the said letter has already been placed.

In the above code I tried to add all the letters in the inclu_Letras tag, but the same one is only adding the last letter and so I can’t make a comparison.

Segue imagem do erro do código

1 answer

0

I’m not sure that’s what you wanted. But it seems to me that you are implementing a little game of a secret word, the player will kick the letter to form the word, and if he repeats the letter, a message informs that the letter has already been chosen. So I implemented something below, see if it satisfies your need:

palavra_secreta = "morango"
letra_digitada=[]
palavra_formada=[None]*len(palavra_secreta)
while(True):
    l_d = str(input("Digite uma letra"))
    if l_d in letra_digitada:
        print('você já escolheu essa letra')
        continue
    else:
        letra_digitada.append(l_d)
        for idx,item in enumerate(palavra_secreta):
            if l_d==item:
                palavra_formada[idx]=item
    if None not in palavra_formada:
        print("Você conseguiu!! A palvara formada é \n {}".format(''.join(palavra_formada)))
        break

Browser other questions tagged

You are not signed in. Login or sign up in order to post.