From d39a0b86059a2ccf702cded14dbcdd793a28fbf8 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 23 Jan 2026 20:20:26 -0800 Subject: [PATCH] Added Palindrome_Checker to the project! --- Palindrome_Checker/palindrome.py | 62 +++++++++++++++++++++ Palindrome_Checker/readme.md | 92 ++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 Palindrome_Checker/palindrome.py create mode 100644 Palindrome_Checker/readme.md diff --git a/Palindrome_Checker/palindrome.py b/Palindrome_Checker/palindrome.py new file mode 100644 index 00000000..29777193 --- /dev/null +++ b/Palindrome_Checker/palindrome.py @@ -0,0 +1,62 @@ +import pyinputplus as pyip +import string + +#Check if word is a palindrome +def word_check(): + strng = pyip.inputStr("\nWhat word would you like to check?\n") #User enters suspected palindrome + strng = strng.lower() #Adjust to lowercase to remove case sensitivity errors + + word_reversed = strng[::-1] #string slicing reverses word & compares it + if strng == word_reversed: + print(f'\nThe word "{strng}" is a palindrome!') + else: + print(f'\nThe word "{strng}" is not a palindrome.') + +def sentence_check(): + strng = pyip.inputStr("\nEnter a sentence to check for palindromes!\n") #User enters suspected palindrome + + strng = strng.lower() #Adjust to lowercase to remove case sensitivity + strng = strng.strip(string.punctuation) #remove punctuation + + word_list = strng.split() + pdromes = [] + + #Iterate over every word in the string to check for palindromes + for word in word_list: + word_reversed = word[::-1] + #store palindromes in their own list + if word == word_reversed: + pdromes.append(word) + + #Get count of total palindromes and list them out + pdrome_count = len(pdromes) + if pdrome_count == 1: + print('\n1 palindrome was detected!') + print(f'-{pdromes[0]}') + elif pdrome_count > 1: + print(f'\n{pdrome_count} palindromes detected!') + print(f'Palindromes: ') + for word in pdromes: + print(f'-{word}') + else: + print('\nNo palindromes were detected.') + +def main(): + choice = pyip.inputInt('\nCheck a [1] Word or [2] Sentence?\n', min=1, max=2) + if choice == 1: + word_check() + else: + sentence_check() + +#Run main program +main() + +#Infinite loop for re-runs + +while True: + restart = pyip.inputYesNo("\nWould you like to check another word or sentence? (y/n): \n") + if restart == "yes": + main() + else: + print("\n--Ending Program---") + break \ No newline at end of file diff --git a/Palindrome_Checker/readme.md b/Palindrome_Checker/readme.md new file mode 100644 index 00000000..601b26a3 --- /dev/null +++ b/Palindrome_Checker/readme.md @@ -0,0 +1,92 @@ +# Palindrome Checker (Python) + +A simple interactive Python program that checks whether a **word** is a palindrome or finds **palindromic words within a sentence**. The program runs in the terminal and allows repeated checks without restarting. + +--- + +## ๐Ÿ“Œ Features + +- Check if a **single word** is a palindrome +- Scan a **sentence** and detect all palindromic words +- Case-insensitive comparisons +- Ignores punctuation in sentence checks +- User-friendly input validation using `pyinputplus` +- Option to rerun the program indefinitely until the user exits + +--- + +## ๐Ÿง  What Is a Palindrome? + +A palindrome is a word that reads the same forwards and backwards. + +Examples: +- racecar +- level +- madam + +--- + +## ๐Ÿ›  Requirements + +- Python 3.x +- pyinputplus module + +Install the required dependency with: + +pip install pyinputplus + +--- + +## โ–ถ๏ธ How to Run + +1. Save the script as `palindrome_checker.py` +2. Open a terminal in the same directory +3. Run: + +python palindrome_checker.py + +--- + +## ๐Ÿงช How It Works + +### Word Check +- Prompts the user to enter a word +- Converts it to lowercase +- Reverses the word using slicing +- Compares the original and reversed word + +### Sentence Check +- Prompts the user to enter a sentence +- Converts it to lowercase +- Removes punctuation +- Splits the sentence into words +- Identifies and lists palindromic words + +--- + +## ๐Ÿ” Program Flow + +1. User selects: + - [1] Check a word + - [2] Check a sentence +2. Program performs the selected check +3. User is asked whether they want to run another check +4. Program continues until the user chooses to exit + +--- + +## ๐Ÿ“‚ Example Output + +Check a [1] Word or [2] Sentence? +1 + +What word would you like to check? +racecar + +The word "racecar" is a palindrome! + +--- + +## ๐Ÿ“„ License + +This project is open-source and free to use for learning or personal projects.