From d1b8384c2ddeeaeea6ecfb02963e6129314265a4 Mon Sep 17 00:00:00 2001 From: Marco Torres Date: Fri, 4 Aug 2023 22:30:53 -0700 Subject: [PATCH] Reto #13 - Python --- .../python/marcoatrs.py | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Retos/Reto #13 - ADIVINA LA PALABRA [Media]/python/marcoatrs.py diff --git a/Retos/Reto #13 - ADIVINA LA PALABRA [Media]/python/marcoatrs.py b/Retos/Reto #13 - ADIVINA LA PALABRA [Media]/python/marcoatrs.py new file mode 100644 index 0000000000..18b82636e4 --- /dev/null +++ b/Retos/Reto #13 - ADIVINA LA PALABRA [Media]/python/marcoatrs.py @@ -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))