From 9c0a8cd46a1048a2f0100a64585aeb8258282ffa Mon Sep 17 00:00:00 2001 From: HumaimaRiaz47 Date: Wed, 22 Oct 2025 19:47:55 +0500 Subject: [PATCH 1/4] Added Hangman game algorithm in Python --- other/hangman_game.py | 162 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 other/hangman_game.py diff --git a/other/hangman_game.py b/other/hangman_game.py new file mode 100644 index 000000000000..4c491368b05d --- /dev/null +++ b/other/hangman_game.py @@ -0,0 +1,162 @@ +""" +hangman.py +A simple command-line Hangman game implemented in Python. + +This program randomly selects a word from a given list and allows the user +to guess it letter by letter. The game ends when the user either correctly +guesses all the letters or exceeds the maximum allowed wrong attempts. + +Usage: + python hangman.py + +Example: + $ python hangman.py + Welcome to Hangman! + _ _ _ _ _ + Enter a letter: a + Correct! + a _ _ _ _ + ... +""" +import random + + +# ----------------------------- # +# HANGMAN STAGES (ASCII ART) +# ----------------------------- # + +words = ( + # Fruits + 'apple', 'orange', 'banana', 'coconut', 'pineapple', 'mango', 'papaya', + 'strawberry', 'blueberry', 'raspberry', 'grape', 'watermelon', 'peach', + 'pear', 'cherry', 'plum', 'kiwi', 'apricot', 'lemon', 'lime', + + # Animals + 'elephant', 'tiger', 'lion', 'giraffe', 'zebra', 'monkey', 'kangaroo', + 'dolphin', 'rabbit', 'panda', 'koala', 'wolf', 'bear', 'fox', 'camel', + 'penguin', 'snake', 'turtle', 'deer', 'leopard', + + # Countries + 'pakistan', 'india', 'china', 'japan', 'brazil', 'canada', 'france', + 'germany', 'australia', 'italy', 'spain', 'egypt', 'turkey', 'russia', + 'mexico', 'norway', 'sweden', 'argentina', 'indonesia', 'nigeria', + + # Colors + 'red', 'blue', 'green', 'yellow', 'purple', 'orange', 'black', 'white', + 'pink', 'brown', 'gray', 'violet', 'indigo', 'silver', 'gold', + + # Computer / Tech + 'python', 'javascript', 'variable', 'function', 'developer', 'keyboard', + 'internet', 'website', 'database', 'algorithm', 'software', 'hardware', + 'network', 'browser', 'program', 'compiler', 'laptop', 'machine', 'coding', + + # Random everyday words + 'school', 'teacher', 'window', 'garden', 'flower', 'butterfly', 'dream', + 'sunshine', 'moonlight', 'family', 'holiday', 'mountain', 'river', + 'forest', 'island', 'cloud', 'ocean', 'rainbow', 'friend', 'love' +) + + +hangman_art: dict[int, tuple[str, str, str]] = { + 0: (" ", " ", " "), + 1: (" o ", " ", " "), + 2: (" o ", " | ", " "), + 3: (" o ", "/| ", " "), + 4: (" o ", "/|\\ ", " "), + 5: (" o ", "/|\\ ", "/ "), + 6: (" o ", "/|\\ ", "/ \\ "), +} + + +def display_man(wrong_guesses: int) -> None: + """ + Display the current hangman stage according to the number of wrong guesses. + + Args: + wrong_guesses (int): Number of incorrect guesses made so far. + """ + print("*****************") + for line in hangman_art[wrong_guesses]: + print(line) + print("*****************") + + +def display_hint(hint: list[str]) -> None: + """ + Display the current state of the guessed word as underscores and letters. + + Args: + hint (list[str]): List containing correctly guessed letters or underscores. + """ + print(" ".join(hint)) + + +def display_answer(answer: str) -> None: + """ + Display the correct word at the end of the game. + + Args: + answer (str): The correct word that was to be guessed. + """ + print(f"The correct word was: {answer}") + + +def main() -> None: + """ + Main function to run the Hangman game. + + The player has up to 6 incorrect guesses to complete the word. + The game ends when the player wins or loses. + """ + answer: str = random.choice(words) + hint: list[str] = ["_"] * len(answer) + wrong_guesses: int = 0 + guessed_letters: set[str] = set() + is_running: bool = True + + print("\n Welcome to Hangman!") + print("Guess the word, one letter at a time.\n") + + while is_running: + display_man(wrong_guesses) + display_hint(hint) + guess = input("Enter a letter: ").lower().strip() + + # Input validation + if len(guess) != 1 or not guess.isalpha(): + print("Invalid input. Please enter a single alphabetic character.\n") + continue + + if guess in guessed_letters: + print(f"'{guess}' has already been guessed.\n") + continue + + guessed_letters.add(guess) + + # Check if the guessed letter is in the word + if guess in answer: + for i, letter in enumerate(answer): + if letter == guess: + hint[i] = guess + print("Correct!\n") + else: + wrong_guesses += 1 + print("Wrong guess!\n") + + # Win condition + if "_" not in hint: + display_man(wrong_guesses) + display_answer(answer) + print("YOU WIN!\n") + is_running = False + + # Lose condition + elif wrong_guesses >= len(hangman_art) - 1: + display_man(wrong_guesses) + display_answer(answer) + print("YOU LOSE!\n") + is_running = False + + +if __name__ == "__main__": + main() \ No newline at end of file From 2a2677286c4dce2d5d5c3ec526a74dd041bd6dd7 Mon Sep 17 00:00:00 2001 From: HumaimaRiaz47 Date: Wed, 22 Oct 2025 20:05:31 +0500 Subject: [PATCH 2/4] add hangman game for Hacktober open source contribution --- other/password.py | 248 +++++++++++++++++++++++++++++++--------------- 1 file changed, 166 insertions(+), 82 deletions(-) diff --git a/other/password.py b/other/password.py index dff1316c049c..51975a422179 100644 --- a/other/password.py +++ b/other/password.py @@ -1,96 +1,180 @@ -import secrets -from random import shuffle -from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation +""" +hangman.py +A simple command-line Hangman game implemented in Python. + +This program randomly selects a word from a given list and allows the user +to guess it letter by letter. The game ends when the user either correctly +guesses all the letters or exceeds the maximum allowed wrong attempts. + +Usage: + python hangman.py + +Example: + $ python hangman.py + Welcome to Hangman! + _ _ _ _ _ + Enter a letter: a + Correct! + a _ _ _ _ + ... +""" + +import random + +# ----------------------------- # +# HANGMAN STAGES +# ----------------------------- # + +words = ( + # Fruits + 'apple', 'orange', 'banana', 'coconut', 'pineapple', 'mango', 'papaya', + 'strawberry', 'blueberry', 'raspberry', 'grape', 'watermelon', 'peach', + 'pear', 'cherry', 'plum', 'kiwi', 'apricot', 'lemon', 'lime', + + # Animals + 'elephant', 'tiger', 'lion', 'giraffe', 'zebra', 'monkey', 'kangaroo', + 'dolphin', 'rabbit', 'panda', 'koala', 'wolf', 'bear', 'fox', 'camel', + 'penguin', 'snake', 'turtle', 'deer', 'leopard', + + # Countries + 'pakistan', 'india', 'china', 'japan', 'brazil', 'canada', 'france', + 'germany', 'australia', 'italy', 'spain', 'egypt', 'turkey', 'russia', + 'mexico', 'norway', 'sweden', 'argentina', 'indonesia', 'nigeria', + + # Colors + 'red', 'blue', 'green', 'yellow', 'purple', 'orange', 'black', 'white', + 'pink', 'brown', 'gray', 'violet', 'indigo', 'silver', 'gold', + + # Computer / Tech + 'python', 'javascript', 'variable', 'function', 'developer', 'keyboard', + 'internet', 'website', 'database', 'algorithm', 'software', 'hardware', + 'network', 'browser', 'program', 'compiler', 'laptop', 'machine', 'coding', + + # Random everyday words + 'school', 'teacher', 'window', 'garden', 'flower', 'butterfly', 'dream', + 'sunshine', 'moonlight', 'family', 'holiday', 'mountain', 'river', + 'forest', 'island', 'cloud', 'ocean', 'rainbow', 'friend', 'love' +) + +hangman_art: dict[int, tuple[str, str, str]] = { + 0: (" ", " ", " "), + 1: (" o ", " ", " "), + 2: (" o ", " | ", " "), + 3: (" o ", "/| ", " "), + 4: (" o ", "/|\\ ", " "), + 5: (" o ", "/|\\ ", "/ "), + 6: (" o ", "/|\\ ", "/ \\ "), +} + + +def display_man(wrong_guesses: int) -> None: + """ + Display the current hangman stage according to the number of wrong guesses. + + Args: + wrong_guesses (int): Number of incorrect guesses made so far. + + >>> display_man(0) + ***************** + + + ***************** + >>> display_man(1) + ***************** + o + + -def password_generator(length: int = 8) -> str: + ***************** """ - Password Generator allows you to generate a random password of length N. - - >>> len(password_generator()) - 8 - >>> len(password_generator(length=16)) - 16 - >>> len(password_generator(257)) - 257 - >>> len(password_generator(length=0)) - 0 - >>> len(password_generator(-1)) - 0 + print("*****************") + for line in hangman_art[wrong_guesses]: + print(line) + print("*****************") + + +def display_hint(hint: list[str]) -> None: """ - chars = ascii_letters + digits + punctuation - return "".join(secrets.choice(chars) for _ in range(length)) - - -# ALTERNATIVE METHODS -# chars_incl= characters that must be in password -# i= how many letters or characters the password length will be -def alternative_password_generator(chars_incl: str, i: int) -> str: - # Password Generator = full boot with random_number, random_letters, and - # random_character FUNCTIONS - # Put your code here... - i -= len(chars_incl) - quotient = i // 3 - remainder = i % 3 - # chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) + - # random_number(digits, i / 3) + random_characters(punctuation, i / 3) - chars = ( - chars_incl - + random(ascii_letters, quotient + remainder) - + random(digits, quotient) - + random(punctuation, quotient) - ) - list_of_chars = list(chars) - shuffle(list_of_chars) - return "".join(list_of_chars) - - # random is a generalised function for letters, characters and numbers - - -def random(chars_incl: str, i: int) -> str: - return "".join(secrets.choice(chars_incl) for _ in range(i)) - - -def is_strong_password(password: str, min_length: int = 8) -> bool: + Display the current state of the guessed word as underscores and letters. + + Args: + hint (list[str]): List containing correctly guessed letters or underscores. + + >>> display_hint(['a', '_', 'p', '_', 'e']) + a _ p _ e """ - This will check whether a given password is strong or not. The password must be at - least as long as the provided minimum length, and it must contain at least 1 - lowercase letter, 1 uppercase letter, 1 number and 1 special character. - - >>> is_strong_password('Hwea7$2!') - True - >>> is_strong_password('Sh0r1') - False - >>> is_strong_password('Hello123') - False - >>> is_strong_password('Hello1238udfhiaf038fajdvjjf!jaiuFhkqi1') - True - >>> is_strong_password('0') - False + print(" ".join(hint)) + + +def display_answer(answer: str) -> None: """ + Display the correct word at the end of the game. - if len(password) < min_length: - return False + Args: + answer (str): The correct word that was to be guessed. - upper = any(char in ascii_uppercase for char in password) - lower = any(char in ascii_lowercase for char in password) - num = any(char in digits for char in password) - spec_char = any(char in punctuation for char in password) + >>> display_answer('python') + The correct word was: python + """ + print(f"The correct word was: {answer}") - return upper and lower and num and spec_char +def main() -> None: + """ + Main function to run the Hangman game. -def main(): - length = int(input("Please indicate the max length of your password: ").strip()) - chars_incl = input( - "Please indicate the characters that must be in your password: " - ).strip() - print("Password generated:", password_generator(length)) - print( - "Alternative Password generated:", - alternative_password_generator(chars_incl, length), - ) - print("[If you are thinking of using this password, You better save it.]") + The player has up to 6 incorrect guesses to complete the word. + The game ends when the player wins or loses. + """ + answer: str = random.choice(words) + hint: list[str] = ["_"] * len(answer) + wrong_guesses: int = 0 + guessed_letters: set[str] = set() + is_running: bool = True + + print("\n Welcome to Hangman!") + print("Guess the word, one letter at a time.\n") + + while is_running: + display_man(wrong_guesses) + display_hint(hint) + guess = input("Enter a letter: ").lower().strip() + + # Input validation + if len(guess) != 1 or not guess.isalpha(): + print("Invalid input. Please enter a single alphabetic character.\n") + continue + + if guess in guessed_letters: + print(f"'{guess}' has already been guessed.\n") + continue + + guessed_letters.add(guess) + + # Check if the guessed letter is in the word + if guess in answer: + for i, letter in enumerate(answer): + if letter == guess: + hint[i] = guess + print("Correct!\n") + else: + wrong_guesses += 1 + print("Wrong guess!\n") + + # Win condition + if "_" not in hint: + display_man(wrong_guesses) + display_answer(answer) + print("YOU WIN!\n") + is_running = False + + # Lose condition + elif wrong_guesses >= len(hangman_art) - 1: + display_man(wrong_guesses) + display_answer(answer) + print("YOU LOSE!\n") + is_running = False if __name__ == "__main__": From 8ff499040cff7b1a39e5d59ca3b837e04082040e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Oct 2025 15:11:25 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/hangman_game.py | 153 ++++++++++++++++++++++++++++++++-------- other/password.py | 160 +++++++++++++++++++++++++++++++++--------- 2 files changed, 249 insertions(+), 64 deletions(-) diff --git a/other/hangman_game.py b/other/hangman_game.py index 4c491368b05d..81de9fa1e3f1 100644 --- a/other/hangman_game.py +++ b/other/hangman_game.py @@ -1,5 +1,5 @@ """ -hangman.py +hangman.py A simple command-line Hangman game implemented in Python. This program randomly selects a word from a given list and allows the user @@ -18,6 +18,7 @@ a _ _ _ _ ... """ + import random @@ -26,34 +27,126 @@ # ----------------------------- # words = ( - # Fruits - 'apple', 'orange', 'banana', 'coconut', 'pineapple', 'mango', 'papaya', - 'strawberry', 'blueberry', 'raspberry', 'grape', 'watermelon', 'peach', - 'pear', 'cherry', 'plum', 'kiwi', 'apricot', 'lemon', 'lime', - - # Animals - 'elephant', 'tiger', 'lion', 'giraffe', 'zebra', 'monkey', 'kangaroo', - 'dolphin', 'rabbit', 'panda', 'koala', 'wolf', 'bear', 'fox', 'camel', - 'penguin', 'snake', 'turtle', 'deer', 'leopard', - - # Countries - 'pakistan', 'india', 'china', 'japan', 'brazil', 'canada', 'france', - 'germany', 'australia', 'italy', 'spain', 'egypt', 'turkey', 'russia', - 'mexico', 'norway', 'sweden', 'argentina', 'indonesia', 'nigeria', - - # Colors - 'red', 'blue', 'green', 'yellow', 'purple', 'orange', 'black', 'white', - 'pink', 'brown', 'gray', 'violet', 'indigo', 'silver', 'gold', - - # Computer / Tech - 'python', 'javascript', 'variable', 'function', 'developer', 'keyboard', - 'internet', 'website', 'database', 'algorithm', 'software', 'hardware', - 'network', 'browser', 'program', 'compiler', 'laptop', 'machine', 'coding', - - # Random everyday words - 'school', 'teacher', 'window', 'garden', 'flower', 'butterfly', 'dream', - 'sunshine', 'moonlight', 'family', 'holiday', 'mountain', 'river', - 'forest', 'island', 'cloud', 'ocean', 'rainbow', 'friend', 'love' + # Fruits + "apple", + "orange", + "banana", + "coconut", + "pineapple", + "mango", + "papaya", + "strawberry", + "blueberry", + "raspberry", + "grape", + "watermelon", + "peach", + "pear", + "cherry", + "plum", + "kiwi", + "apricot", + "lemon", + "lime", + # Animals + "elephant", + "tiger", + "lion", + "giraffe", + "zebra", + "monkey", + "kangaroo", + "dolphin", + "rabbit", + "panda", + "koala", + "wolf", + "bear", + "fox", + "camel", + "penguin", + "snake", + "turtle", + "deer", + "leopard", + # Countries + "pakistan", + "india", + "china", + "japan", + "brazil", + "canada", + "france", + "germany", + "australia", + "italy", + "spain", + "egypt", + "turkey", + "russia", + "mexico", + "norway", + "sweden", + "argentina", + "indonesia", + "nigeria", + # Colors + "red", + "blue", + "green", + "yellow", + "purple", + "orange", + "black", + "white", + "pink", + "brown", + "gray", + "violet", + "indigo", + "silver", + "gold", + # Computer / Tech + "python", + "javascript", + "variable", + "function", + "developer", + "keyboard", + "internet", + "website", + "database", + "algorithm", + "software", + "hardware", + "network", + "browser", + "program", + "compiler", + "laptop", + "machine", + "coding", + # Random everyday words + "school", + "teacher", + "window", + "garden", + "flower", + "butterfly", + "dream", + "sunshine", + "moonlight", + "family", + "holiday", + "mountain", + "river", + "forest", + "island", + "cloud", + "ocean", + "rainbow", + "friend", + "love", ) @@ -159,4 +252,4 @@ def main() -> None: if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/other/password.py b/other/password.py index 51975a422179..e85fa5b94bb8 100644 --- a/other/password.py +++ b/other/password.py @@ -1,5 +1,5 @@ """ -hangman.py +hangman.py A simple command-line Hangman game implemented in Python. This program randomly selects a word from a given list and allows the user @@ -26,34 +26,126 @@ # ----------------------------- # words = ( - # Fruits - 'apple', 'orange', 'banana', 'coconut', 'pineapple', 'mango', 'papaya', - 'strawberry', 'blueberry', 'raspberry', 'grape', 'watermelon', 'peach', - 'pear', 'cherry', 'plum', 'kiwi', 'apricot', 'lemon', 'lime', - - # Animals - 'elephant', 'tiger', 'lion', 'giraffe', 'zebra', 'monkey', 'kangaroo', - 'dolphin', 'rabbit', 'panda', 'koala', 'wolf', 'bear', 'fox', 'camel', - 'penguin', 'snake', 'turtle', 'deer', 'leopard', - - # Countries - 'pakistan', 'india', 'china', 'japan', 'brazil', 'canada', 'france', - 'germany', 'australia', 'italy', 'spain', 'egypt', 'turkey', 'russia', - 'mexico', 'norway', 'sweden', 'argentina', 'indonesia', 'nigeria', - - # Colors - 'red', 'blue', 'green', 'yellow', 'purple', 'orange', 'black', 'white', - 'pink', 'brown', 'gray', 'violet', 'indigo', 'silver', 'gold', - - # Computer / Tech - 'python', 'javascript', 'variable', 'function', 'developer', 'keyboard', - 'internet', 'website', 'database', 'algorithm', 'software', 'hardware', - 'network', 'browser', 'program', 'compiler', 'laptop', 'machine', 'coding', - - # Random everyday words - 'school', 'teacher', 'window', 'garden', 'flower', 'butterfly', 'dream', - 'sunshine', 'moonlight', 'family', 'holiday', 'mountain', 'river', - 'forest', 'island', 'cloud', 'ocean', 'rainbow', 'friend', 'love' + # Fruits + "apple", + "orange", + "banana", + "coconut", + "pineapple", + "mango", + "papaya", + "strawberry", + "blueberry", + "raspberry", + "grape", + "watermelon", + "peach", + "pear", + "cherry", + "plum", + "kiwi", + "apricot", + "lemon", + "lime", + # Animals + "elephant", + "tiger", + "lion", + "giraffe", + "zebra", + "monkey", + "kangaroo", + "dolphin", + "rabbit", + "panda", + "koala", + "wolf", + "bear", + "fox", + "camel", + "penguin", + "snake", + "turtle", + "deer", + "leopard", + # Countries + "pakistan", + "india", + "china", + "japan", + "brazil", + "canada", + "france", + "germany", + "australia", + "italy", + "spain", + "egypt", + "turkey", + "russia", + "mexico", + "norway", + "sweden", + "argentina", + "indonesia", + "nigeria", + # Colors + "red", + "blue", + "green", + "yellow", + "purple", + "orange", + "black", + "white", + "pink", + "brown", + "gray", + "violet", + "indigo", + "silver", + "gold", + # Computer / Tech + "python", + "javascript", + "variable", + "function", + "developer", + "keyboard", + "internet", + "website", + "database", + "algorithm", + "software", + "hardware", + "network", + "browser", + "program", + "compiler", + "laptop", + "machine", + "coding", + # Random everyday words + "school", + "teacher", + "window", + "garden", + "flower", + "butterfly", + "dream", + "sunshine", + "moonlight", + "family", + "holiday", + "mountain", + "river", + "forest", + "island", + "cloud", + "ocean", + "rainbow", + "friend", + "love", ) hangman_art: dict[int, tuple[str, str, str]] = { @@ -76,15 +168,15 @@ def display_man(wrong_guesses: int) -> None: >>> display_man(0) ***************** - - + + ***************** >>> display_man(1) ***************** - o - - + o + + ***************** """ From fc9ab83d783cbf44c7dfe78a253f099a028b0eda Mon Sep 17 00:00:00 2001 From: HumaimaRiaz47 Date: Wed, 22 Oct 2025 20:13:50 +0500 Subject: [PATCH 4/4] add doctest to Added doctest for main() in Hangman game --- other/password.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/other/password.py b/other/password.py index 51975a422179..99e18829db6e 100644 --- a/other/password.py +++ b/other/password.py @@ -126,6 +126,9 @@ def main() -> None: The player has up to 6 incorrect guesses to complete the word. The game ends when the player wins or loses. + + >>> isinstance(main(), type(None)) + True """ answer: str = random.choice(words) hint: list[str] = ["_"] * len(answer)