2012-05-05 29 views
-1
fno = input() 
myList = list(fno) 
sum = 0 
for i in range(len(fno)): 
    if myList[0:] == myList[:0]: 
    continue 
print (myList) 

我想做一個數字迴文。 如:Python中的迴文代碼

input(123) 
print(You are wrong) 
input(12121) 
print(you are right) 

請指導我如何在python.Its迴文不完整的代碼,請建議我下一步是什麼。

感謝

+0

[Python的反向()爲迴文]的可能重複(http://stackoverflow.com/questions/5202533/python-reverse-for-palindromes) – jamylak

+0

究竟你的意思是什麼使一個迴文數'?舉個例子說明你期望拿到什麼和拿出什麼。 –

+1

你的例子沒有說明太多。你說'做',但似乎顯示*檢查它,看看它是否是迴文。如果您確實需要像您的示例那樣檢查,我的答案纔有效。 –

回答

6

我想,給你的代碼,你要檢查迴文,不會讓一個。

有一些與你的代碼的問題,但在短期,也可以降低到

word = input() 
if word == "".join(reversed(word)): 
    print("Palidrome") 

讓我們來談談你的代碼,這並沒有太大的意義:

fno = input() 
myList = list(fno) #fno will be a string, which is already a sequence, there is no need to make a list. 
sum = 0 #This goes unused. What is it for? 
for i in range(len(fno)): #You should never loop over a range of a length, just loop over the object itself. 
    if myList[0:] == myList[:0]: #This checks if the slice from beginning to the end is equal to the slice from the beginning to the beginning (nothing) - this will only be true for an empty string. 
     continue #And then it does nothing anyway. (I am presuming this was meant to be indented) 
print (myList) #This will print the list of the characters from the string. 
+5

'if word == word [:: - 1]' – DrTyrsa

+0

@DTTyrsa這不是一個好的解決方案。最重要的是,對於不懂語法的人來說,它是神祕的,而且它也可能會慢一些。 –

+2

你能證明它「可能較慢」嗎?我認爲我們爲那些知道語法的人編寫代碼。 – DrTyrsa

-1
x=raw_input("enter the string") 
while True: 
    if x[0: ]==x[::-1]: 
     print 'string is palindrome' 
     break 
    else: 
     print 'string is not palindrome' 
     break 
0
str=input('Enter a String') 
print('Original string is : ',str) 
rev=str[::-1] 
print('the reversed string is : ',rev) 
if(str==rev): 
    print('its palindrome') 
else: 
    print('its not palindrome')