I'm writing a program that checks a txt file with a list of words (one word per line). I'm just this side of done, I think.

def StripPunctuation(s):
    answer=""
    for c in s:
        if c.islower() or c.isupper():
            answer=answer+c.lower()
    return answer
def Reverse(s):
    answer=""
    for c in s:
        answer=c+answer
    return answer
def IsPalindrome(s):
    s=StripPunctuation(s)
    if s==Reverse(s):
        return True
    else:
        return False
def main():
    F=open("wordList.txt","r")
    for line in F:
        word=line.strip("\n")
    if IsPalindrome(word):
        print "%s"%word

main()

Instead of a list I just get the last line printed if it's a palindrome. And nothing if it's not. What do I need to add to get the entire list?

Print "word" within the bottom for loop, and print "s" within IsPalindrome. It is considered poor style to have one isolated call to main() at the end of the program, which is just a function that calls statements that are better left outside, because in large programs you look to the end for anything that gets executed, and then have to search through many, many lines of code to try and find "main()".

You can indent the announcement inside the loop.

Don't forget about the reversed() BIF here, it could save you a few lines:

def is_pal(word):
    rev_word=''
    for letter in reversed(word):
        rev_word+=letter
    if word==rev_word:
        return True
    else:
        return False

likewise, it's much simpler and easier to maintain.

pyguys version ignores case, non-letter issue, here is the pyTonyc version, as OP did it by himself:

def is_palindrome(sentence):
    lower_letters = ''.join(c.lower() for c in sentence if c.isalpha())
    return lower_letters == ''.join(reversed(lower_letters))

for test_case in ('A man, a plan, a canal, Panama!','Saippuakivikauppias', 'Not Tony',
                  'Aku pajatti: "Olla voiko totta, makeessa lukupaketissa kassit." Eka puku, Lasse, eka matto, Tokio, valloittaja Puka.',
                  'Mikael, Aira, Mika, Elli, Hanna, Anu, Anni, Milla, Allu, Leo, Janne, Jari, Anne, Heli, Ina, Jori, Mimmi, Immo, Tomi, Toni, Tia, Kalle, Olavi, Mari, Rami, Valo, Ella, Kai, Tino, Timo, Tommi, Immi, Miro, Jani, Ile, Henna, Ira, Jenna, Joel, Ulla, Alli, Minna, Una, Anna, Hille, Aki, Maria, Lea, Kim'):
    print('%r %s palindrome.' % (test_case, 'is' if is_palindrome(test_case) else 'is not'))

EDIT: Sample of Finnish palindrome sentence and name list.

pyguys version ignores case, non-letter issue, here is the pyTonyc version, as OP did it by himself:

def is_palindrome(sentence):
    lower_letters = ''.join(c.lower() for c in sentence if c.isalpha())
    return lower_letters == lower_letters[::-1]

for test_case in ('A man, a plan, a canal, Panama!','Saippuakivikauppias', 'Not Tony'):
    print('%r %s palindrome' % (test_case, 'is' if is_palindrome(test_case) else 'is not'))

good point, I was just going with this statement

(one word per line)

but yes, you are right, that was does cover alot more ground.

None of these really answer my question. I want to read every line not just the final line.

hmmm...I feel like this is one of those things that is pretty well documented, here's one option:

>>> f=open('the.txt','r')
>>> text=f.readlines()
>>> text
['this\n', 'is \n', 'a \n', 'file']

There's a class on google code for python, check it out it's not too bad.

Python is indent sensitive and unfortunately there is no other way than correct indention. You are reading everything, just printing out of loop.

None of these really answer my question. I want to read every line not just the final line.

You could have tried to figure something yourself with the help you have gotten.
I am gone write a soultion of what you want.

So here is my version,i bring in some new factor like [::-1] the pythonic reverse.
And string.punctuation to remove punctuation,because a palindrome can also be a number.

#p.txt
racecar.
1881!!
test
radar
import string

def is_palindrom(word):
    word = ''.join(c.lower() for c in word if c not in string.punctuation)
    if word == word[::-1]:
        return True
    return False

with open('p.txt') as f:
    for numb,line in enumerate(f, 1):
        if is_palindrom(line.strip()):
            print 'Line %d is a palindrome' % numb
        else:
            print 'Line %d is not a palindrome' % numb

'''Out-->
Line 1 is a palindrome
Line 2 is a palindrome
Line 3 is not a palindrome
Line 4 is a palindrome
'''

This is a quick and easy way to check if a word is palindrome.

'''
Created on Feb 20, 2012

@author: sin
'''
def lff(file):
    line = open(file).read().splitlines()
    for i in range(0, len(line)):
        if str(line[i]).lower() == str(line[i]).lower()[::-1]:
            print "'" + str(line[i]) + "'", "is palindrome."
        else:
            print "'" + str(line[i]) + "'", "is not palindrome."

file = str(raw_input("Filename: ")) + str('.txt')   
if __name__ == "__main__":
    lff(file)

Hope I helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.