
🌐 También en: English · Deutsch · Français
Llevo unos días trasteando con Python, sobre todo teoría: variables, bucles, condicionales y los sospechosos habituales. Y como todo programador novato con dos dedos de frente, decidí que mi primer proyecto “de verdad” tenía que ser algo épico.
Así que, naturalmente… el ahorcado.
Es el reto perfecto para principiantes:
- Practicas variables llevando la cuenta de las letras adivinadas y los intentos fallidos.
- Usas bucles para el desarrollo de la partida.
- Añades unas pizcas de condicionales para decidir quién vive y quién muere (bueno, en ASCII).
Tras un poco de prueba y error (y unos cuantos “Ah, claro, Python distingue entre mayúsculas y minúsculas”), conseguí que mi versión funcionara. Lo reconozco: dejé que ChatGPT “tuneara” algunas partes, pero oye, así se aprende más rápido, ¿no?
Qué hace
- Elige una palabra al azar de una lista predefinida
- Dibuja un monigote en ASCII con cada fallo
- Te deja adivinar letras sueltas o la palabra entera
- Gestiona mayúsculas, minúsculas y letras repetidas
- Presume de mis crecientes superpoderes en Python
Por qué me encanta
No es solo un juego: es una pequeña cápsula del tiempo de dónde estoy ahora mismo en mi camino como programador.
Dentro de unos meses lo miraré y pensaré: “Vaya… cuánto he avanzado”.
Pero por ahora, es pura alegría ver cómo mi terminal se llena de guiones bajos, arte ASCII y mensajes de victoria (o derrotas trágicas).
Reflexiones finales
Si estás empezando con Python, haz un ahorcado. En serio. Es el patio de recreo perfecto para experimentar con bucles, lógica y la magia del ASCII. Y si consigues que tu código quede limpio y funcione sin bugs, ya has ganado.
library.py
words = ["Apfel", "Banane", "Mango", "Kirsche", "Pfirsich", "Linux", "Kernel", "Docker", "Ansible", "Python"]
logo = ['''
_
| |
| |__ __ _ _ __ __ _ _ __ ___ __ _ _ __
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|___/''']
HANGMANPICS = ['''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
/|\ |
|
|
=========''', '''
+---+
| |
O |
/|\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\ |
/ \ |
|
=========''']
main.py
# =============================================================================
# Hangman Game
#
# File: main.py
# Author: Raphael Münch
# Email: you@example.com
# Website: https://www.apt-upgrade.me
#
# Created: 2025-08-15
# Last Updated: 2025-08-15
#
# Description:
# A simple Hangman game played in the console.
# Includes ASCII art, word guessing, and configurable max attempts.
#
# Copyright (c) 2025 Raphael Münch. All rights reserved.
#
# License:
# This software is licensed under the MIT License.
# You may use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, provided the copyright notice
# above appears in all copies.
#
# =============================================================================
import library
import os
import random
DEBUG = False
while True:
# Clear the console (Windows = 'cls', others = 'clear')
os.system('cls' if os.name == 'nt' else 'clear')
print(library.logo[0])
chosen_word = random.choice(library.words)
word_lower = chosen_word.lower()
# Create a mask: "_" for each letter in the word
mask = ["_"] * len(chosen_word)
guessed_letters = "" # Letters guessed in the current round
wrong_attempts = 0 # Count of wrong guesses
max_attempts = len(library.HANGMANPICS) - 1 # e.g., 6 wrong guesses allowed
found_letter = False
while True:
print(f"Your guesses: {guessed_letters}")
print(f"Current word: {' '.join(mask)}")
if DEBUG:
print(f"Chosen word: {chosen_word}")
print(f"Attempt: {wrong_attempts}/{max_attempts}")
choice = input("Enter a letter or guess the word: ").strip()
# Whole word guess
if len(choice) > 1:
if choice.lower() == word_lower:
mask = list(chosen_word) # Reveal the whole word
print("You win!!!")
break
else:
wrong_attempts += 1
print(f"\n\n{library.HANGMANPICS[min(wrong_attempts, max_attempts)]}")
if wrong_attempts >= max_attempts:
print("\n\aYou've lost!!!")
print(f"The word was: {chosen_word}")
break
continue
# Single letter guess
if not choice:
continue
guess = choice.lower()
# Skip if letter already guessed
if guess in guessed_letters:
print("Already guessed. Try another letter.")
continue
guessed_letters += guess
# Check if guess matches any letter in the word
found_letter = False
for i, ch in enumerate(word_lower):
if guess == ch:
mask[i] = chosen_word[i] # Preserve original case
found_letter = True
if not found_letter:
wrong_attempts += 1
print(f"\n\n{library.HANGMANPICS[min(wrong_attempts, max_attempts)]}")
if wrong_attempts >= max_attempts:
print("\n\aYou've lost!!!")
print(f"The word was: {chosen_word}")
break
if "".join(mask).lower() == word_lower:
print("You win!!!")
break
answer = input("Do you want to play again (y/n)? ").strip().lower()
if answer != "y":
break






