2012-05-05 3 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

あなたの例はあまり明確にはなりません。あなたは 'make'と言っていますが、それが回文かどうかを調べる*ように見えるようです。あなたの例のようにチェックしているのであれば、私の答えが働きます。 –

答えて

5

スライス表記は、ここでは有用です:

>>> "malayalam"[::-1] 
'malayalam' 
>>> "hello"[::-1] 
'olleh' 

が良いintroductためExplain Python's slice notationを参照してください。イオン。

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

@DrTyrsaこれは良い解決策ではありません。最も重要なことは、構文を知らない人にはわかりにくく、潜在的には遅いことです。 –

+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') 
関連する問題