Palindrome program using python

Discussion in 'Mac Programming' started by tngisaga, Feb 8, 2010.

  1. tngisaga macrumors member

    Oct 11, 2006
    San Jose, CA
    I have an assignment in which I have to write a palindrome program. I searched online for some advice, but I couldn't find any without the codes using the def function. I'm only allowed to use for and while loops, if, elif, and else statements. I have the program ask the users to input a word or phrase. In order to have it recognize a palindrome, I tell the computer that position 0 and position -1 of the word have to be the same.

    In addition, the program also has to look for palindromes with a word or phrase so there's some indexing going on as well. For example, radar also has the palindrome "ada" in it as well. For all the palindromes it finds, it has to print the starting and ending position of that palindrome.

    Here is what I have so far:

    string raaw_input("Please enter a word or phrase: ")
    string_len len(string)
    "The length of the string is "string_len

    #Here is the part where the program looks for palindromes.  

    for i in range(string_len):
    j in range(stringlen-1i+1, -1):
    while (string[i+k] == string[j-k]):
    if (j-0) or (i+== string_len):
    So if we tested, for example, "attadatta," it gets some of the palindromes but misses some others. I don't know how to fix these bugs. If anyone could lend a hand and tell me what I'm doing wrong, I would really appreciate it. Thanks everyone!
  2. angelwatt Moderator emeritus


    Aug 16, 2005
    The palindrome assignment has come up here before. MRoogle showed me a few, though not necessarily for Python.
  3. tngisaga thread starter macrumors member

    Oct 11, 2006
    San Jose, CA
    Yeah, I did a search and I saw those other programs as well. Like you said, none are for python. This is my first programming language so I really don't know how to extract any useful info from programs written in other languages and use them here.
  4. chown33 macrumors 604

    Aug 9, 2009
    As posted, your code doesn't run at all.

    The errors aren't insurmountable, but it suggests it wasn't directly copied and pasted from a runnable source.

    If that were true, then "deeded" would be a palindrome because it starts and ends with the letter "d" and the substring "de". Your code isn't doing what you described, but if it were, it would be wrong.

    Your code is doing something else wrong, and I suggest you debug it by giving it a short input string that you know isn't a palindrome, but which it says is. It's not hard to discover these.

    Anyway, with some suitable texts known, your next step is to walk through your code by hand, calculating and comparing, and following the exact flow of the code. It shouldn't take more than a couple manual iterations to find the problem. This is a time-tested debugging process, and is a basic skill that all programmers will need.

    Also, instead of the vague generality "it gets some of the palindromes but misses some others", you should post actual output so we can see what happens:
    Please enter a word or phrase: deeded
    The length of the string is  6
    [ 0 ] [ 2 ]     dee
    [ 2 ] [ 4 ]     ede
  5. tngisaga thread starter macrumors member

    Oct 11, 2006
    San Jose, CA
    Thanks for the reply chown33.

    I went back and rewrote the program step-by-step, and I got it to work the way I wanted. Problem Solved.

Share This Page