我用文档测试创建了一个非常简单的回文检查器。
我对最后一个 doctest 有问题。它失败并且没有执行ignorecase=True
. 我无法弄清楚为什么最后一次测试失败了。
代码:
# This Python file uses the following encoding: utf-8
def isPalindrome(s, ignorecase=False):
"""
>>> type(isPalindrome("bob"))
<type 'bool'>
>>> isPalindrome("abc")
False
>>> isPalindrome("bob")
True
>>> isPalindrome("a man a plan a canal, panama")
True
>>> isPalindrome("A man a plan a canal, Panama")
False
>>> isPalindrome("A man a plan a canal, Panama", ignorecase=True)
True
"""
# Create an empty string "onlyLetters"
# Loop over all characters in the string argument, and add each
# character which is a letter to "onlyletters"
# Reverse "onlyletters" and test if this is equal to "onlyletters"
#s = ""
news = ""
for eachLetter in s:
if eachLetter.isalpha():
news += eachLetter
#print news
onlyLetters = news
#print onlyLetters
onlyletters = news[::-1]
#print onlyletters
if onlyLetters == onlyletters:
return True
else:
return False