Reputation: 53
I have an if/else statement in python, and I want to do nothing when it goes to the else statement.
I am reading a list of words, finding the palindromes (eg 'abba') and printing them out. It currently prints out the entire list of words, and I know the is_palindrome function is working correctly.
def first(word):
return word[0]
def last(word):
return word[-1]
def middle(word):
return word[1:-1]
def is_palindrome(word):
#print(word)
if len(word) >1:
if first(word) == last(word):
#print(word)
#print(middle(word))
return is_palindrome(middle(word))
else:
return False
else:
return True
try:
words = open("/usr/share/dict/words","r")
for line in words:
line.strip()
#print line
if is_palindrome(line) == True:
print(line)
else
words.close()
except:
print("File open FAILED")
I'd appreciate any insight you could give me. Thanks.
Upvotes: 0
Views: 270
Reputation: 1002
'line.strip()' by itself won't change line. This ought to work.
for line in words:
line = line.strip()
if is_palindrome(line) == True:
print(line)
words.close()
Upvotes: 9