该文件接受答案不打印



我的英语和python知识非常低.....因此,我的问题是写一个单allindrome识别器的版本,该版本接受用户的文件名,读取每一行,并在屏幕上打印到屏幕上的线。

我的代码:

that is was i write to the textfile: "anna" "keek" "toot" "poop" "eguzki"
def palindrome():
    with open('home/me/pytho/textfile.txt', 'r') as f:
             for i in f:
                if i == i[:-1]:
                    new = i
             print new
palindrome()

但是我什么也没来...请用简单的单词回答,因为我的代码不是肠子,谢谢!

示例文本文件

$ cat ex.txt 
"anna", "keek", "toot", "poop", "eguzki"

python代码

def is_palindrome(word):
    return word == word[::-1]
with open('ex.txt') as f:
    words = f.readlines()[0].split(", ")
    for word in words:
        print word, "palindrome" if is_palindrome(word) else "not palindrome"

执行代码。

$ python palindrome.py 
"anna" palindrome
"keek" palindrome
"toot" palindrome
"poop" palindrome
"eguzki" not palindrome

最新更新