Skip to content

Commit

Permalink
Reto #13 - Python
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoatrs committed Aug 5, 2023
1 parent ede61cf commit d1b8384
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions Retos/Reto #13 - ADIVINA LA PALABRA [Media]/python/marcoatrs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import random
import sys

secrets = [
"secreta",
"palabra",
"programacion",
"intentos",
"dificultad",
"armonia",
"comadreja",
"tomarnos",
"vibracion",
"siuuuuuuuuu",
"oficial",
]


def init(word: str) -> str:
hidden_chars = int(len(word) * 0.6)
hidden_word = list(word)
count = 0
while count < hidden_chars:
idx = random.randint(0, len(word) - 1)
if hidden_word[idx] == "_":
continue
hidden_word[idx] = "_"
count += 1
return "".join(hidden_word)


def indexes(word: list, input: str) -> list:
return [(idx, w) for idx, w in enumerate(word) if input == w]


def game(word: str, tries: int = 3):
hidden_word = init(word)

while tries > 0:
print(f"Adivina la palabra: Tienes {tries} intentos")
print(hidden_word)
play = input()
if len(word) == len(play):
if word == play:
print("Ganaste")
sys.exit(0)
print("Error")
tries -= 1
continue
if len(play) != 1:
print("La palabra debe ser del mismo tamaño")
tries -= 1
continue
if not play in word:
print("Error")
tries -= 1
continue
idx = indexes(list(word), play)
new_hidden_word = list(hidden_word)
for i, w in idx:
new_hidden_word[i] = w
hidden_word = "".join(new_hidden_word)
if word == hidden_word:
print(f"Ganaste: {word}")
sys.exit(0)
print(f"Perdiste, era {word}")


game(random.choice(secrets))

0 comments on commit d1b8384

Please sign in to comment.