Palindromes

To practice using for loops and the range() function, you will be creating a program that takes in a string from the user and checks if it is a palindrome.

DO NOT use any of Python's built-in string functions (other than upper() or lower() when solving this lab.





Creating your new program

Now you are going to write a simple Python program called palindrome.py.

Once you are in your lab6 folder, open your file for editing by typing

       emacs palindrome.py

The first thing you should do in your new file is create and fill out the comment header block at the top of your file. Here is a template (it is text, so feel free to copy and paste it into your file):

# File:        palindrome.py
# Author:      YOUR NAME
# Date:        TODAY'S DATE
# Section:     YOUR SECTION NUMBER
# E-mail:      USERNAME@umbc.edu
# Description: YOUR DESCRIPTION GOES HERE AND HERE
#              YOUR DESCRIPTION CONTINUED SOME MORE
# Collaboration: During lab, collaboration between students is allowed,
#                although I understand I still must understand the material
#                and complete the assignment myself. 





Reversing a string

For the first part of your code, you should ask the user to input a string, and store it. Then, using a for loop, you program should recreate that string in reverse. Make sure that you save your result in a new variable, because you will need it for the next part of this lab.

You must use a for loop for this part of the lab.

THINK: What is your input? What is your desired output? What process should you use to solve this problem?

If you get stuck, there are hints for a number of common problems under the "Additional Help" link in the left hand menu. Try to complete the program on your own before turning to these hints.





Checking for palindromes

You should only move on to this part of the lab if you have completed the "Reversing a string" section, and tested that it works correctly.
(Try giving your program "abcdef," and see if the output is the correct answer of "fedcba.")

In the previous part of this lab, you wrote a for loop to reverse the string given by the user (and stored the reversed version of the string in a separate variable from the original). With this complete, you can write the rest of your program, to make it a simple palindrome checker.

As a reminder, a palindrome is a string that is written the same way forwards as it is backwards. Examples of strings that are palindromes are:

Check if the two words (the original and the reverse) are palindromes, and tell the user by printing your result to the screen. (Remember to tell them if it's not a palindrome as well!)





Sample output

Here is a sample run of the program, to give you an idea of what your program could look like. The exact text of the prompts may differ, but the eventual result printed out should be the same as the sample.

User input is shown in blue.

linux2[6]% scl enable python33 bash
bash-4.1$ python palindrome.py
Enter a string: racecar
racecar is a palindrome

bash-4.1$ python palindrome.py
Enter a string: kittycat
kittycat is not a palindrome

bash-4.1$ python palindrome.py
Enter a string: ProfGibsonLovesDogs
ProfGibsonLovesDogs is not a palindrome

bash-4.1$ python palindrome.py
Enter a string: deified
deified is a palindrome

Remember to enable Python 3 before running your program!





If you get stuck, there are hints for a number of common problems under the "Additional Help" link in the left hand menu. Try to complete the program on your own before turning to these hints.