我下載了一個在線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