I have made a game of guess the word, and I have a piece of code that verifies if the user has already entered a letter before, it looks like this:
if letra in actual:
clear()
print('La letra ya se encuentra en la palabra')
espacio()
print(' '*10, ' '.join(actual))
intentos -= 1
espacio()
print('Intentos fallidos restantes: ', intentos, '\n')
where
clear = lambda: os.system('cls')
def espacio(cantidad=1):
print('\n'*cantidad)
and assuming that the word to guess is 'ARBUSTO', it is:
palabra = 'ARBUSTO'
actual = list('_'*len(palabra))
basically 'current' builds a list with '_' as many letters as there are in the word, which I will replace later as the correct letters are entered.
The thing is that the print () that is between the clear () and the space () never shows the message it should: 'The letter is already in the word', in fact it does not matter what argument the print has , or if I write another one, if you put it there it is never shown on the screen, I have similar structures throughout the program and all of them work with the exception of this one.
Here the complete code
import os
import sys
import getpass
def espacio(cantidad=1):
print('\n'*cantidad)
clear = lambda: os.system('cls')
palabra = getpass.getpass('\nIngrese la palabra a adivinar: ').upper()
intentos = int(input('Número de intentos fallidos permitidos: '))
clear()
espacio(2)
actual = list('_'*len(palabra))
print(' '*10, ' '.join(actual))
espacio(2)
while True:
if intentos == 0:
clear()
print('La palabra era:')
espacio()
print(' '*10,' '.join(list(palabra)))
espacio()
print('Has perdido \n')
sys.exit()
break
letra = input('Ingrese una letra: ').upper()
cantidad = palabra.count(letra)
if letra in palabra:
posicion = palabra.index(letra)
izq = len(palabra[:posicion])
der = len(palabra[posicion+1:])
if letra in actual:
clear()
print('La letra ya se encuentra en la palabra')
espacio()
print(' '*10, ' '.join(actual))
intentos -= 1
espacio()
print('Intentos fallidos restantes: ', intentos, '\n')
actual[posicion] = letra
if cantidad is not 1:
for i in range(cantidad-1):
posicion = palabra.index(letra, posicion+1)
izq = len(palabra[:posicion])
der = len(palabra[posicion+1:])
actual[posicion] = letra
if actual.count('_') == 0:
clear()
print('La palabra es:')
espacio()
print(' '*10,' '.join(actual))
espacio()
print('Felicidades! Has ganado\n')
sys.exit()
clear()
espacio()
print(' '*10, ' '.join(actual))
espacio()
print('Intentos fallidos restantes: ', intentos)
espacio()
else:
clear()
print('La letra no se encuentra en la palabra')
espacio()
print(' '*10, ' '.join(actual))
intentos -= 1
espacio()
print('Intentos fallidos restantes: ', intentos, '\n')