def isPalindrome(word):
l = len(word)
for i in range(l/2):
if(word[i] != word[i+l-1]):
return 0
return 1
def main():
print("\n\n\tTo Check if the word is a Palindrome\n\n")
word = raw_input("Enter a word : ")
if(isPalindrome(word) == 1):
print("It is a Palindrome")
elif:
print("It is not a Palindrome")
main()
在我看來,在程序中一切正常。python中的迴文邏輯:這個程序有什麼問題?
Enter a word : madam
Traceback (most recent call last):
File "temp.py", line 16, in <module>
File "temp.py", line 6, in isPalindrome
IndexError: string index out of range
什麼是錯的是不是一個合適的問題,你收到了什麼錯誤,你有什麼期望輸出 - 什麼你會得到嗎?例如。在'python3'範圍內(1/2)''會出錯,因爲'/'會返回一個'float'(使用''''')是你得到的錯誤? – AChampion