-1 python for-loop if-statement palindrome
fno = input()
myList = list(fno)
sum = 0
for i in range(len(fno)):
if myList[0:] == myList[:0]:
continue
print (myList)
Run Code Online (Sandbox Code Playgroud)
我想做一个回文数.例如:
input(123)
print(You are wrong)
input(12121)
print(you are right)
Run Code Online (Sandbox Code Playgroud)
请指导我如何在python中制作回文.如果没有完整的代码请告诉我下一步是什么.
谢谢
我认为,鉴于你的代码,你想检查一个回文,而不是一个.
您的代码存在许多问题,但简而言之,它可以减少到
word = input()
if word == "".join(reversed(word)):
print("Palidrome")
Run Code Online (Sandbox Code Playgroud)
让我们谈谈你的代码,这没有多大意义:
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.
Run Code Online (Sandbox Code Playgroud)
切片表示法在这里很有用:
>>> "malayalam"[::-1]
'malayalam'
>>> "hello"[::-1]
'olleh'
Run Code Online (Sandbox Code Playgroud)
有关详细介绍,请参阅解释Python的切片表示法.
| 归档时间: |
|
| 查看次数: |
3330 次 |
| 最近记录: |