-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

Slice notation is useful here:

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

See Explain Python's slice notation for a good introduction.

于 2012-05-05T10:23:33.000 に答える
-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 に答える