Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Palindrome_Checker/palindrome.py
Original file line number Diff line number Diff line change
@@ -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
92 changes: 92 additions & 0 deletions Palindrome_Checker/readme.md
Original file line number Diff line number Diff line change
@@ -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.