palindrome4.py
 

The following program takes a string of characters from the user and determines if the string is a palindrome, printing the result.  If you don't know where to start, paste the following text in your favorite text editor (such as notepad, if you are a Windows user) and save it as palindrome4.py in a folder that contains other .py programs that work on your computer.
 

# stacktest and queuetest start out as empty lists.

stacktest = []
queuetest = []

# Obtain a string of characters from the user.

userstring = raw_input("Please type in a word to find out if it is a palindrome. ")

# Place the characters in userstring in the two empty lists.  One will be in reverse order.

for char in userstring:
    stacktest.insert(0, char)
    queuetest.append(char)

# Compare the two lists to determine if they are equivalent.  If so, you have a palindrome.

if stacktest == queuetest:
    print userstring + " is a palindrome."
else:
    print userstring + " is not a palindrome."

The program is called from within the interactive interpreter by importing the module, as follows.  The program instructs the user on how to proceed.
 
>>>import palindrome4
Please type in a word to find out if it is a palindrome. radar
radar is a palindrome.
>>>


Back to the MS_PIGgie Home Page