我試圖做一個程序,檢查一個單詞是否是一個迴文,我已經得到了迄今爲止,它與單詞有偶數量的數字。如果字母數量很奇怪,我知道如何讓它做些什麼,但我不知道如何找出一個數字是否奇怪。有沒有簡單的方法來查找一個數字是奇數還是偶數?檢查一個數字是奇數還是蟒蛇
僅供參考,這是我的代碼:
a = 0
while a == 0:
print("\n \n" * 100)
print("Please enter a word to check if it is a palindrome: ")
word = input("?: ")
wordLength = int(len(word))
finalWordLength = int(wordLength/2)
firstHalf = word[:finalWordLength]
secondHalf = word[finalWordLength + 1:]
secondHalf = secondHalf[::-1]
print(firstHalf)
print(secondHalf)
if firstHalf == secondHalf:
print("This is a palindrom")
else:
print("This is not a palindrom")
print("Press enter to restart")
input()
感謝
我不知道該怎麼你會在Python表達這一點,但(字長模2)== 1如果將一個單詞有字符數爲奇數的是真實的。 – 2014-02-17 19:05:52