Simple I/O problem can't get solved

nickgan.sps at windowslive.com nickgan.sps at windowslive.com
Fri Jun 21 04:57:48 EDT 2013


Greetings to everybody,

I have recently started to solve my first Python problems but I came across to this:
Write a version of a palindrome recogniser that accepts a file name from the user, reads each line, and prints the line to the screen if it is a palindrome.(by http://www.ling.gu.se/~lager/python_exercises.html)

I think I coded it correctly but still I get no output. I do not have any errors I just do not get any output.

This is the code:

***
import is_palindrome

filename = raw_input('Enter a file: ') # A text file
f = open(filename)
while True:
	line = f.readline()
	if len(line) == 0:
		break
	elif is_palindrome.is_palindrome(line):	
		print line,
f.close()
***

I have coded the is_palindrome module before and it works very well.
Here is this module's code:

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

def replace_all(text, dic):
	for i, j in dic.iteritems():
		text = text.replace(i, j)
	return text

def is_palindrome(text):
	return text == reverse(text)
	
points = {'.':'', ',':'', '!':'', ';':'', '?':'', '/':'', '\\':''}

something = raw_input('Enter text: ').lower().replace(' ', '')
replace = replace_all(something, points)
if is_palindrome(replace):
	print 'Yes, it is a palindrome:', reverse(replace)
else:
	print 'No, it is not a palindrome:', reverse(replace)
***

What should I do to output only the palindrome lines from the text file?



More information about the Python-list mailing list