我需要在Python的切片表示法上有一个很好的解释(引用是一个加号).
对我来说,这种符号需要一点点提升.
它看起来非常强大,但我还没有完全了解它.
我有一个代码来检查一个单词是否是回文:
str = input("Enter the string")
l = len(str)
p = l-1
index = 0
while index < p:
if str[index] == str[p]:
index = index + 1
p = p-1
print("String is a palindrome")
break
else:
print("string is not a palindrome")
Run Code Online (Sandbox Code Playgroud)
如果输入了一个单词,例如:rotor,我希望程序检查这个单词是否是回文并给出输出为“给定的单词是回文”。
但我面临的问题是,程序首先检查 r 和 r 并打印“给定的单词是回文”,然后检查 o 和 o 并打印“给定的单词是回文”。它会在检查单词时多次打印结果。
我希望结果只传递一次。如何更改代码?
我想找到通过两个3位数字相乘得到的最大回文.
我开始时a和b均为999,并且在发生的每次乘法时递减a和b.
a = 999 #Defining Variables
b = 999
for i in range (1000):
c= a*b #multiply a to b
if int(str(c)[::-1]) == c:
print c
a = a-1 #decrement the value of a
c=a*b #multiply a by the decremented a
if int(str(c)[::-1]) == c:
print c
b = b-1 #decrement b so that both a and b have been decremented
Run Code Online (Sandbox Code Playgroud)
结果出现了698896,289982,94249,69696 ......其中698896是第一个数字.目前我还在试图找出我所缺少的东西.