-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ahorcado.py
executable file
·125 lines (125 loc) · 4 KB
/
Ahorcado.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import random
AHORCADO = ['''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
/|\ |
|
|
=========''', '''
+---+
| |
O |
/|\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\ |
/ \ |
|
=========''']
palabras = 'valoracion aprenderpython comida juego python web imposible variable curso volador cabeza reproductor mirada escritor billete lapicero celular valor revista gratuito disco voleibol anillo estrella'.split()
def buscarPalabraAleat(listaPalabras):
# Esta funcion retorna una palabra aleatoria.
palabraAleatoria = random.randint(0, len(listaPalabras) - 1)
return listaPalabras[palabraAleatoria]
def displayBoard(AHORCADO, letraIncorrecta, letraCorrecta, palabraSecreta):
print(AHORCADO[len(letraIncorrecta)])
print ("")
fin = " "
print ('Letras incorrectas:', fin)
for letra in letraIncorrecta:
print (letra, fin)
print ("")
espacio = '_' * len(palabraSecreta)
for i in range(len(palabraSecreta)): # Remplaza los espacios en blanco por la letra bien escrita
if palabraSecreta[i] in letraCorrecta:
espacio = espacio[:i] + palabraSecreta[i] + espacio[i+1:]
for letra in espacio: # Mostrará la palabra secreta con espacios entre letras
print (letra, fin)
print ("")
def elijeLetra(algunaLetra):
# Devuelve la letra que el jugador ingreso. Esta función hace que el jugador ingrese una letra y no cualquier otra cosa
while True:
print ('Adivina una letra:')
letra = input()
letra = letra.lower()
if len(letra) != 1:
print ('Introduce una sola letra.')
elif letra in algunaLetra:
print ('Ya has elegido esa letra ¿Qué tal si pruebas con otra?')
elif letra not in 'abcdefghijklmnopqrstuvwxyz':
print ('Elije una letra.')
else:
return letra
def empezar():
# Esta funcion devuelve True si el jugador quiere volver a jugar, de lo contrario devuelve False
print ('Quieres jugar de nuevo? (Si o No)')
return input().lower().startswith('s')
print ('A H O R C A D O')
letraIncorrecta = ""
letraCorrecta = ""
palabraSecreta = buscarPalabraAleat(palabras)
finJuego = False
while True:
displayBoard(AHORCADO, letraIncorrecta, letraCorrecta, palabraSecreta)
# El usuairo elije una letra.
letra = elijeLetra(letraIncorrecta + letraCorrecta)
if letra in palabraSecreta:
letraCorrecta = letraCorrecta + letra
# Se fija si el jugador ganó
letrasEncontradas = True
for i in range(len(palabraSecreta)):
if palabraSecreta[i] not in letraCorrecta:
letrasEncontradas = False
break
if letrasEncontradas:
print ('¡Muy bien! La palabra secreta es "' + palabraSecreta + '"! ¡Has ganado!')
finJuego = True
else:
letraIncorrecta = letraIncorrecta + letra
# Comprueba la cantidad de letras que ha ingresado el jugador y si perdió
if len(letraIncorrecta) == len(AHORCADO) - 1:
displayBoard(AHORCADO, letraIncorrecta, letraCorrecta, palabraSecreta)
print ('¡Se ha quedado sin letras!\nDespues de ' + str(len(letraIncorrecta)) + ' letras erroneas y ' + str(len(letraCorrecta)) + ' letras correctas, la palabra era "' + palabraSecreta + '"')
finJuego = True
# Pregunta al jugador si quiere jugar de nuevo
if finJuego:
if empezar():
letraIncorrecta = ""
letraCorrecta = ""
finJuego = False
palabraSecreta = buscarPalabraAleat(palabras)
else:
break