Problem with my Hangman game - Python

Asked

Viewed 456 times

1

print("*** Jogo da forca ***\n")
print("*** Feito por Igor! ***\n")
palavraSecreta = input("Entre com a palavra secreta: ")
letrasDescobertas = []
percorrer = 0
contador = int(input("Entre com o número de chances: "))

for i in range(len(palavraSecreta)):
    if palavraSecreta[i] == " ":
        letrasDescobertas.append(" ")
    else:
        letrasDescobertas.append("-")
print("".join(letrasDescobertas))

acertou = False

while acertou is False:
    letra = input("Digite a letra de seu chute: ")

    for i in range(len(palavraSecreta)):
        if letra == palavraSecreta[i]:
            letrasDescobertas[i] = letra
            print("".join(letrasDescobertas))

    if letra not in palavraSecreta:
        percorrer += 1
        print("Chances restantes:", contador - percorrer)
        if percorrer == contador:
            print("Você perdeu!")
            break

    if "-" not in letrasDescobertas:
        print("Você ganhou com %d erros!" % percorrer)
        acertou = True

My riddle is pao: If I type d, f, g. I should show the wrong letters like this: d, f, g. But the output I have with this code I did is only the last letter typed. For example: Typed d, will appear d. Typed f, will appear only f, instead of d, f. How to tidy up? Thanks

  • It could make the code easier?

  • Okay, the code is up (the indentation got a little wrong here on the site)

1 answer

1


Good, now I really understood what the problem was and now I edited to fix.

Follows the code:

Pay attention to what I did, because I don’t even know what I really did. But the result is perfect.

print("*** Jogo da forca ***\n")
palavraSecreta = input("Entre com a palavra secreta: ")
palavraSecreta = palavraSecreta.split(' ')

letrasDescobertas = []

for i in palavraSecreta:
    for b in i:
        letrasDescobertas.append('_')
    letrasDescobertas.append(' ') # A cada intervalo é adicionado um espaço

acertou = False

while acertou is False:
    letra = input("Digite a letra de seu chute: ")
    x = 0
    for i in palavraSecreta: # Não precisa por zero no começo, o padrão é sempre zero

        for b in i:
            if letra == b:
                if letrasDescobertas[x] == ' ': # É preciso incrementa aqui por que senão o espaço vai ser substituído 
                    x += 1
                letrasDescobertas[x] = letra
            x += 1
        x += 1 # Quando ele muda para o outra palavra é necessário incrementar +1

    print(''.join(letrasDescobertas))


    if '_' not in letrasDescobertas: # Mudei isso aqui também, ficou melhor.
        acertou = True

print("Parabéns!")

Exit:

>>> *** Jogo da forca ***
>>> 
>>> Entre com a palavra secreta: um teste
>>> Digite a letra de seu chute: u
>>> u_ _____ 
>>> Digite a letra de seu chute: m
>>> um _____ 
>>> Digite a letra de seu chute: t
>>> um t__t_ 
>>> Digite a letra de seu chute: e
>>> um te_te 
>>> Digite a letra de seu chute: s
>>> um teste 
>>> Parabéns!

Letters already used:

Add this after the while acertou is False:

while acertou is False:
    letra = input("Digite a letra de seu chute: ")

    if letra in usadas: # Adicione isto
          print('\nVocê já falou essa letra!\n') # Adicione isto
          continue # Adicione isto

    usadas.append(letra) # Adicione isto

Exit:

>>> *** Jogo da forca ***
>>> 
>>> Entre com a palavra secreta: antony
>>> Digite a letra de seu chute: a
>>> a_____
>>> Digite a letra de seu chute: a
>>> 
>>> Você já falou essa letra!
>>> 
>>> Digite a letra de seu chute:
  • So, man, the problem is, I wanted you to have the dash or underline it, to show the player the number of words. But the problem is that when the word has space, the program counts space as letter, then it adds a "-" to the space. I could tell?

  • Okay, I edited, I didn’t quite understand.

  • Wow, I got all confused with this code... what did you do there? hahaha

  • I managed to do it in a more simplified way :D, I looked at your code and I got the idea, and it worked xD. Take a look there, I modified my code above

  • So, I gave one more implemented in my code. Now I would like you to have a list that saved all the wrong letters and printable on the screen. I could almost do that, but this list only shows the last contents of the list, instead of showing the whole list. Would you help me? Code above

  • In which case it would be letters that have already been used, correct? If so, I updated the answer.

  • It did not work here Antony. And the intention was not only to show the msg, but the letters that have already been used. I made available my code all above

  • For this just type print(used), well here it is quiet bro.

  • Does it have to make your code all available please? Thank you bro!

  • The topic will get very long, just replace the last part I quoted, and put usadas = [] above While, no more than that.

  • It worked here brother! Now one last question if you can help me. Because when I put the "used = []" here, the code doesn’t work?: while hit is False: letter = input("Type the letter of your kick: ") used = []

  • And when I put the used one above While hit is false the code works? I was in doubt

  • Why usadas=[]will always be resetting when the while goes back to the start and above While it will store the data without resetting, understood?

  • Puts guy, truth hahaha. I get it, sorry for the Noob question, and thank you so much for the help, thank you so much! :)

  • Nothing, no one is born knowing, is living and learning. :)

Show 10 more comments

Browser other questions tagged

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