diff --git a/Calculator/main.py b/Calculator/main.py index 599cbc0..95b9948 100644 --- a/Calculator/main.py +++ b/Calculator/main.py @@ -7,10 +7,12 @@ def exponent(ne1, ne2): """This function will return the power of two numbers""" return ne1**ne2 + def nth_root(nr1, n): """Getting the nth root of nr1""" return nr1**(1/n) + def subtract(ns1, ns2): """ This function will return the difference of two numbers """ return ns1 - ns2 @@ -26,20 +28,27 @@ def divide(nd1, nd2): return nd1 / nd2 +def modulo(nmo1, nmo2): + """ This function will return the remainder of the division of two numbers""" + return nmo1 % nmo2 + + operations = { "+": add, "-": subtract, "*": multiply, "/": divide, "^": exponent, - "√": nth_root + "√": nth_root, + "%": modulo } def calculator(): """ This function contains the code that will work as you wish to proceed \ an operation """ - num1 = float(input("What's the first number?(to pick √, hold alt + 251 (on numpad)): ")) + num1 = float( + input("What's the first number?(to pick √, hold alt + 251 (on numpad)): ")) for symbol in operations: print(symbol) should_continue = True diff --git a/HangMan/main.py b/HangMan/main.py index fae27bd..19bd3b7 100644 --- a/HangMan/main.py +++ b/HangMan/main.py @@ -1,38 +1,200 @@ """ The random module help us choose a random word from a list of words and the sys module helps us in exiting the program """ import random -import sys +import string -words_list = ['python', 'code', 'tech', 'programming', 'love', 'hate', 'passion'] -chosen_word = random.choice(words_list) -print(chosen_word) -correct_word = [] -for _ in chosen_word: - correct_word.append('_') -print(' '.join(correct_word)) +def display_hangman(lives): + """Display hangman ASCII art based on remaining lives""" + stages = [ + """ + ------ + | | + | O + | /|\\ + | / \\ + | + -------- + """, + """ + ------ + | | + | O + | /|\\ + | / + | + -------- + """, + """ + ------ + | | + | O + | /|\\ + | + | + -------- + """, + """ + ------ + | | + | O + | /| + | + | + -------- + """, + """ + ------ + | | + | O + | | + | + | + -------- + """, + """ + ------ + | | + | O + | + | + | + -------- + """, + """ + ------ + | | + | + | + | + | + -------- + """ + ] + return stages[lives] -LIVES = 6 -while True: - guess = input("Guess a letter: ") +def get_valid_input(): + """Get and validate user input""" + while True: + guess = input("\nGuess a letter: ").lower().strip() - if guess in correct_word: - print("You have already guessed this letter.") - LIVES -= 1 + if len(guess) != 1: + print("Please enter exactly one letter.") + continue - if guess not in chosen_word: - LIVES -= 1 - print("You guessed a wrong letter.") + if not guess.isalpha(): + print("Please enter a valid letter (a-z).") + continue - for index in range(len(correct_word)): - letter = chosen_word[index] + return guess - if letter == guess: - correct_word[index] = letter - print(' '.join(correct_word)) - if LIVES == 0: - sys.exit("You Lose.") - if '_' not in correct_word: - sys.exit("You Win.") +def play_hangman(): + """Main game function""" + # Word categories for better gameplay + word_categories = { + 'programming': ['python', 'javascript', 'algorithm', 'function', 'variable', 'debugging', 'compiler'], + 'technology': ['computer', 'internet', 'software', 'hardware', 'database', 'network', 'security'], + 'general': ['rainbow', 'butterfly', 'adventure', 'mystery', 'journey', 'treasure', 'champion'] + } + + # Game setup + print("\n" + "="*50) + print(" "*15 + "WELCOME TO HANGMAN!") + print("="*50) + + # Choose category (optional feature) + print("\nCategories: programming, technology, general") + category = input( + "Choose a category (or press Enter for random): ").lower().strip() + + if category in word_categories: + words_list = word_categories[category] + print(f"\nCategory: {category.upper()}") + else: + words_list = [word for sublist in word_categories.values() + for word in sublist] + print("\nCategory: RANDOM") + + # Initialize game variables + chosen_word = random.choice(words_list) + word_display = ['_'] * len(chosen_word) + guessed_letters = set() + incorrect_letters = set() + lives = 6 + + # Game loop + while lives > 0 and '_' in word_display: + # Display game state + print("\n" + "-"*40) + print(display_hangman(lives)) + print(f"Word: {' '.join(word_display)}") + print(f"Lives remaining: {lives}") + + if incorrect_letters: + print(f"Incorrect letters: {', '.join(sorted(incorrect_letters))}") + + # Get player input + guess = get_valid_input() + + # Check if letter was already guessed + if guess in guessed_letters: + print(f"You've already guessed '{guess}'. Try a different letter.") + continue + + guessed_letters.add(guess) + + # Check if guess is in the word + if guess in chosen_word: + print(f"Good guess! '{guess}' is in the word.") + # Update word display + for i, letter in enumerate(chosen_word): + if letter == guess: + word_display[i] = letter + else: + lives -= 1 + incorrect_letters.add(guess) + print(f"Sorry, '{guess}' is not in the word.") + if lives > 0: + print( + f"You have {lives} {'life' if lives == 1 else 'lives'} left.") + + # Game over + print("\n" + "="*50) + if '_' not in word_display: + print("🎉 CONGRATULATIONS! YOU WON! 🎉") + print(f"The word was: {chosen_word.upper()}") + print( + f"You solved it with {lives} {'life' if lives == 1 else 'lives'} remaining!") + else: + print(display_hangman(0)) + print("💀 GAME OVER! YOU LOST! 💀") + print(f"The word was: {chosen_word.upper()}") + print("="*50) + + return play_again() + + +def play_again(): + """Ask if player wants to play again""" + while True: + choice = input( + "\nWould you like to play again? (yes/no): ").lower().strip() + if choice in ['yes', 'y']: + return True + elif choice in ['no', 'n']: + print("\nThanks for playing! Goodbye!") + return False + else: + print("Please enter 'yes' or 'no'.") + + +def main(): + """Main program entry point""" + while play_hangman(): + pass + + +if __name__ == "__main__": + main() diff --git a/Password-Generator/main.py b/Password-Generator/main.py index ae0d58b..6e5182e 100644 --- a/Password-Generator/main.py +++ b/Password-Generator/main.py @@ -1,161 +1,258 @@ -""" The random module help us get random values of our choice """ +""" Enhanced password generator with better practices and features """ import random -from colorama import Fore +import string +import pyperclip # Optional: for clipboard functionality +from colorama import Fore, init + +# Initialize colorama for cross-platform color support +init(autoreset=True) def main(): - """This is the main function and it calls all the other function - depending on user_choice""" - user_choice = input( - "Type 'weak' to get a weak password, 'medium' to get a medium one \ -and 'strong' to get a strong password: " - ).lower() - if user_choice == "weak": - weak_password() - elif user_choice == "medium": - medium_password() - elif user_choice == "strong": - strong_password() - else: - print(Fore.RED + "Invalid Input!") + """Main function that handles user interaction and password generation""" + print(Fore.CYAN + "="*50) + print(Fore.CYAN + " PASSWORD GENERATOR") + print(Fore.CYAN + "="*50 + "\n") + + while True: + user_choice = input( + Fore.YELLOW + "Choose password strength:\n" + " 1) 'weak' - lowercase letters only\n" + " 2) 'medium' - letters and numbers\n" + " 3) 'strong' - letters, numbers, and symbols\n" + " 4) 'custom' - customize your password\n" + " 5) 'exit' - quit the program\n" + + Fore.WHITE + "Your choice: " + ).lower().strip() + + if user_choice in ['weak', '1']: + weak_password() + elif user_choice in ['medium', '2']: + medium_password() + elif user_choice in ['strong', '3']: + strong_password() + elif user_choice in ['custom', '4']: + custom_password() + elif user_choice in ['exit', '5', 'quit']: + print(Fore.GREEN + "Thank you for using Password Generator!") + break + else: + print(Fore.RED + "Invalid input! Please try again.\n") + + if user_choice in ['weak', 'medium', 'strong', 'custom', '1', '2', '3', '4']: + another = input( + Fore.YELLOW + "\nGenerate another password? (yes/no): ").lower() + if another not in ['yes', 'y']: + print(Fore.GREEN + "Thank you for using Password Generator!") + break + print() # Add spacing + + +def get_valid_length(min_length=4, max_length=128): + """Get valid password length from user""" + while True: + try: + length = int( + input(Fore.WHITE + f"Enter password length ({min_length}-{max_length}): ")) + if min_length <= length <= max_length: + return length + else: + print( + Fore.RED + f"Please enter a number between {min_length} and {max_length}") + except ValueError: + print(Fore.RED + "Please enter a valid number") + + +def get_valid_input(prompt, max_value): + """Get valid integer input from user""" + while True: + try: + value = int(input(Fore.WHITE + prompt)) + if 0 <= value <= max_value: + return value + else: + print( + Fore.RED + f"Please enter a number between 0 and {max_value}") + except ValueError: + print(Fore.RED + "Please enter a valid number") + + +def display_password(password, strength): + """Display the generated password with formatting""" + strength_colors = { + 'Weak': Fore.RED, + 'Medium': Fore.YELLOW, + 'Strong': Fore.GREEN, + 'Custom': Fore.CYAN + } + + print("\n" + "="*50) + print(f"{strength_colors[strength]}Password Strength: {strength}") + print(f"{Fore.WHITE}Your password: {strength_colors[strength]}{password}") + print("="*50) + + # Optional: Copy to clipboard (requires pyperclip) + try: + pyperclip.copy(password) + print(Fore.GREEN + "✓ Password copied to clipboard!") + except: + pass # pyperclip might not be installed def weak_password(): - """This function will generate an weak password for you""" - letters = [ - "a", - "b", - "c", - "d", - "d", - "e", - "f", - "g", - "h", - "i", - "j", - "k", - "l", - "m", - "n", - "o", - "p", - "q", - "r", - "s", - "t", - "u", - "v", - "w", - "x", - "y", - "z", - ] - user_choice = int(input("How long you need your password to be: ")) - password = random.choices(letters, k=user_choice) - print("Here is your password: " + Fore.RED + "".join(password)) + """Generate a weak password using only lowercase letters""" + length = get_valid_length() + password = ''.join(random.choices(string.ascii_lowercase, k=length)) + display_password(password, 'Weak') def medium_password(): - """This function will generate a medium password for you""" - letters = [ - "a", - "b", - "c", - "d", - "d", - "e", - "f", - "g", - "h", - "i", - "j", - "k", - "l", - "m", - "n", - "o", - "p", - "q", - "r", - "s", - "t", - "u", - "v", - "w", - "x", - "y", - "z", - ] - numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] - letter_choice = int(input("How much letters do you want in your password: ")) - number_choice = int(input("How much numbers do you want in your password: ")) - letter_password = random.choices(letters, k=letter_choice) - number_password = random.choices(numbers, k=number_choice) - password = letter_password + number_password - random.shuffle(password) - print("Here is your password: " + Fore.RED + "".join(password)) + """Generate a medium password using letters and numbers""" + print(Fore.CYAN + "\nMedium Password Configuration:") + length = get_valid_length() + + # Use both uppercase and lowercase letters with numbers + characters = string.ascii_letters + string.digits + password = ''.join(random.choices(characters, k=length)) + + # Ensure at least one letter and one number + if not any(c.isalpha() for c in password): + password = password[:-1] + random.choice(string.ascii_letters) + if not any(c.isdigit() for c in password): + password = password[:-1] + random.choice(string.digits) + + # Shuffle to randomize + password_list = list(password) + random.shuffle(password_list) + password = ''.join(password_list) + + display_password(password, 'Medium') def strong_password(): - """This function will generate a strong password for you""" - letters = [ - "a", - "b", - "c", - "d", - "d", - "e", - "f", - "g", - "h", - "i", - "j", - "k", - "l", - "m", - "n", - "o", - "p", - "q", - "r", - "s", - "t", - "u", - "v", - "w", - "x", - "y", - "z", - ] - numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] - symbols = [ - "~", - "@", - "#", - "$", - "%", - "^", - "&", - "*", - "(", - ")", - "?", - "/", - "<", - ">", - "|", - ] - - letter_choice = int(input("How much letters do you want in your password: ")) - number_choice = int(input("How much numbers do you want in your password: ")) - symbol_choice = int(input("How much symbols do you want in your password: ")) - letter_password = random.choices(letters, k=letter_choice) - number_password = random.choices(numbers, k=number_choice) - symbol_password = random.choices(symbols, k=symbol_choice) - password = letter_password + number_password + symbol_password + """Generate a strong password using letters, numbers, and symbols""" + print(Fore.CYAN + "\nStrong Password Configuration:") + # Strong passwords should be at least 8 chars + length = get_valid_length(min_length=8) + + # Define character sets + symbols = "!@#$%^&*()_+-=[]{}|;:,.<>?" + all_characters = string.ascii_letters + string.digits + symbols + + # Generate password ensuring complexity + password = [] + # Ensure at least one of each type + password.append(random.choice(string.ascii_lowercase)) + password.append(random.choice(string.ascii_uppercase)) + password.append(random.choice(string.digits)) + password.append(random.choice(symbols)) + + # Fill the rest randomly + for _ in range(length - 4): + password.append(random.choice(all_characters)) + + # Shuffle to randomize random.shuffle(password) - print("Here is your password: " + Fore.RED + "".join(password)) + password = ''.join(password) + + display_password(password, 'Strong') + + +def custom_password(): + """Generate a custom password with user-specified requirements""" + print(Fore.CYAN + "\nCustom Password Configuration:") + + # Get user preferences + use_lowercase = input( + Fore.WHITE + "Include lowercase letters? (yes/no): ").lower() in ['yes', 'y'] + use_uppercase = input( + Fore.WHITE + "Include uppercase letters? (yes/no): ").lower() in ['yes', 'y'] + use_digits = input( + Fore.WHITE + "Include numbers? (yes/no): ").lower() in ['yes', 'y'] + use_symbols = input( + Fore.WHITE + "Include symbols? (yes/no): ").lower() in ['yes', 'y'] + + # Build character pool + char_pool = "" + if use_lowercase: + char_pool += string.ascii_lowercase + if use_uppercase: + char_pool += string.ascii_uppercase + if use_digits: + char_pool += string.digits + if use_symbols: + char_pool += "!@#$%^&*()_+-=[]{}|;:,.<>?" + + if not char_pool: + print(Fore.RED + "You must select at least one character type!") + return + + length = get_valid_length() + + # Generate password + password = [] + + # Ensure at least one of each selected type + if use_lowercase and length >= 1: + password.append(random.choice(string.ascii_lowercase)) + if use_uppercase and length >= 2: + password.append(random.choice(string.ascii_uppercase)) + if use_digits and length >= 3: + password.append(random.choice(string.digits)) + if use_symbols and length >= 4: + password.append(random.choice("!@#$%^&*()_+-=[]{}|;:,.<>?")) + + # Fill remaining length + remaining = length - len(password) + password.extend(random.choices(char_pool, k=remaining)) + + # Shuffle + random.shuffle(password) + password = ''.join(password) + + display_password(password, 'Custom') + + +def check_password_strength(password): + """Analyze and display password strength metrics""" + score = 0 + feedback = [] + + # Length check + if len(password) >= 12: + score += 2 + feedback.append(Fore.GREEN + "✓ Good length") + elif len(password) >= 8: + score += 1 + feedback.append(Fore.YELLOW + "○ Moderate length") + else: + feedback.append(Fore.RED + "✗ Too short") + + # Character variety checks + if any(c.islower() for c in password): + score += 1 + feedback.append(Fore.GREEN + "✓ Contains lowercase") + if any(c.isupper() for c in password): + score += 1 + feedback.append(Fore.GREEN + "✓ Contains uppercase") + if any(c.isdigit() for c in password): + score += 1 + feedback.append(Fore.GREEN + "✓ Contains numbers") + if any(c in "!@#$%^&*()_+-=[]{}|;:,.<>?" for c in password): + score += 2 + feedback.append(Fore.GREEN + "✓ Contains symbols") + + # Display feedback + print("\nPassword Strength Analysis:") + for item in feedback: + print(f" {item}") + + if score >= 6: + print(Fore.GREEN + f"\nStrength Score: {score}/7 - STRONG") + elif score >= 4: + print(Fore.YELLOW + f"\nStrength Score: {score}/7 - MEDIUM") + else: + print(Fore.RED + f"\nStrength Score: {score}/7 - WEAK") if __name__ == "__main__":