Skip to content

Instantly share code, notes, and snippets.

@Elixsa
Created February 8, 2025 23:46
Show Gist options
  • Save Elixsa/93cd7f9f989322d67755b7ab574e47c1 to your computer and use it in GitHub Desktop.
Save Elixsa/93cd7f9f989322d67755b7ab574e47c1 to your computer and use it in GitHub Desktop.
2.5.1.7 LAB: Palindromes
#Do you know what a palindrome is?
#It's a word which look the same when read forward and backward. For example, "kayak" is a palindrome, while "loyal" is not.
#Your task is to write a program which:
#asks the user for some text;
##Note: assume that an empty string isn't a palindrome;
#treat upper- and lower-case letters as equal;
#spaces are not taken into account during the check - treat them as non-existent;
#there are more than a few correct solutions - try to find more than one.
#Test your code using the data we've provided.
#Test data
#Sample input:
#Ten animals I slam in a net
#Sample output:
#It's a palindrome
#Sample input:
#Eleven animals I slam in a net
#Sample output:
#It's not a palindrome
checkpali = input("Please enter a phrase to check: \n")
#strip all irrelevant characters and spaces
#convert all characters to lower case
checkpali = checkpali.lower()
#print(checkpali)
palifiltered = ''
for letter in checkpali:
if letter.isalpha():
palifiltered += letter
#print(palifiltered)
ispalindrome = True
for space in range(int(len(palifiltered)/2)):
if palifiltered[space]==palifiltered[-(space+1)]:
continue
else:
ispalindrome = False
break
#print(ispalindrome)
if ispalindrome == True:
print("It's a palindrome")
elif ispalindrome == False:
print("It's not a palindrome")
else:
print("Sorry, something went wrong.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment