我下载了一个在线MIT课程,要求学生创建一个函数来测试一个字符串是否是回文。他们提到len
并且只取一小段字符串。据我了解,我既不使用任务,但我的代码似乎工作。 有什么我失踪?家庭作业 - 家中自学 - Palindrome
def test_word():
question = input("Do you want to see if a word or sentence is a
palindrome? (y/n)")
question = question.lower()
if question == "y":
sample = input("Provide test word or sentence: \n>>>")
sample = sample.lower()
print(is_palindrome(sample))
test_word()
elif question == "n":
print("Goodbye!")
quit()
else:
print("Y for yes or N for no, please.")
test_word()
def is_palindrome(x):
# Use negative 1 step to reverse order
y = x[::-1]
if x == y:
return True
elif x != y:
return False
test_word()
如果你的代码正在工作有什么问题? – Rahul