-1

I have modified the following function to return if the given string is a Palindrome. I also have it excluding capitalization, spaces, and punctuation. How can i modify this to return "false" if the user enters a non string.

import string

def remove_punctuations(word):
    return "".join(i.lower() for i in word if i in string.ascii_letters)

def reverse(text):
    return text[::-1]

def is_palindrome(text):
    text = remove_punctuations(text)
    return text==reverse(text)

# where I am entering the string
something = "12.5"

if (is_palindrome(something)):
   print("True")
else:
   print("False")

# some test cases
>>> isPalindrome("alula")
True
>>> isPalindrome("love")
False
>>> isPalindrome(12)
False
>>> isPalindrome(12.21)
False

Also how can i adjust this code so i can input is_palindrome(something), instead of manually entering "something"

  • As a design suggestion: **don't**. Just document that it expects a string, and let it crash however it crashes if given something non-stringy. – wim Mar 20 '18 at 22:49

1 Answers1

0

You have multiple options here. You can use input(), raw_input(), argparse, optparse to take input from the user.
Regarding the type of input, you can checkout the isinstance() method of python.