Indexerror: list assignment index out of range

Asked

Viewed 495 times

1

I tried to solve the exercise https://python.nilo.pro.br/exercicios/capitulo%2007/exercicio-07-09.html and even the template presents an error:

Traceback (most recent call last):  File "teste.py", line 59, in <module>
    linhas[3][3] = "O"
IndexError: list assignment index out of range

The complete code is this:

palavras = [
          "casa",
          "bola",
          "mangueira",
          "uva",
          "quiabo",
          "computador",
          "cobra",
          "lentilha",
          "arroz"
     ]

índice = int(input("Digite um numero:"))
palavra = palavras[ (índice * 776) % len(palavras)]
for x in range(100):
     print()
digitadas = []
acertos = []
erros = 0

linhas_txt = """
X==:==
X  :
X
X
X
X
=======

"""

linhas = []

for linha in linhas_txt.splitlines():
     linhas.append(list(linha))

while True:
     senha = ""
     for letra in palavra:
         senha += letra if letra in acertos else "."
     print(senha)
     if senha == palavra:
         print("Você acertou!")
         break
     tentativa = input("\nDigite uma letra:").lower().strip()
     if tentativa in digitadas:
         print("Você já tentou esta letra!")
         continue
     else:
         digitadas += tentativa
         if tentativa in palavra:
               acertos += tentativa
         else:
               erros += 1
               print("Você errou!")
               if erros == 1:
                    linhas[3][3] = "O"
               elif erros == 2:
                    linhas[4][3] = "|"
               elif erros == 3:
                    linhas[4][2] = "\\"
               elif erros == 4:
                    linhas[4][4] = "/"
               elif erros == 5:
                    linhas[5][2] = "/"
               elif erros == 6:
                    linhas[5][4] = "\\"

     for l in linhas:
          print("".join(l))
     if erros == 6:
         print("Enforcado!")
         print("A palavra secreta era: %s" % palavra)
         break

Does anyone have any idea where the problem is?

  • I put it in here and it made the same mistake, trying to figure out what’s wrong.

  • If the answer met your goals, be sure to accept it.

1 answer

1


The string representing the gallows needs to have empty spaces so that it can be replaced by the hangman’s parts as the errors occur. In the code fragment below add 6 spaces in each line next to each X from the third line.

linhas_txt = """
X==:==
X  :
X      
X      
X      
X      
=======

"""

So every letter you miss, the empty space will be replaced by the body part of the hanged man, according to the fragment below:

if erros == 1:
    linhas[3][3] = "O"
elif erros == 2:
    linhas[4][3] = "|"
elif erros == 3:
    linhas[4][2] = "\\"
elif erros == 4:
    linhas[4][4] = "/"
elif erros == 5:
    linhas[5][2] = "/"
elif erros == 6:
    linhas[5][4] = "\\"
  • 1

    I hadn’t really realized that I needed to create a space to insert str. Thank you very much for the attention.

Browser other questions tagged

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