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

我想做一个数字回文。例如:

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

请指导我如何在 python 中制作回文。它不完整的代码请建议我下一步做什么。

谢谢

4

4 回答 4

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.
于 2012-05-05T10:21:47.983 回答
5

切片符号在这里很有用:

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

请参阅解释 Python 的切片表示法以获得很好的介绍。

于 2012-05-05T10:23:33.000 回答
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')
于 2017-11-28T02:47:36.577 回答
-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
于 2016-07-03T18:24:27.793 回答